Canman2005 Posted May 18, 2007 Share Posted May 18, 2007 Hi all I have a script which uploads a CSV file and then updates any information contained in a SQL database, so if there is a new address for a user in the CSV file, then it updates the SQL database with the new information. The code I use is if (isset($_FILES['documentfile']['size']) && $_FILES['documentfile']['size'] > 100) { $upfile = ''.$random."_".$_FILES['documentfile']['name']; if (!copy($_FILES['documentfile']['tmp_name'], $upfile)) { print "Could not move file into upload directory"; exit; } $link = mysql_connect("localhost","***","***"); $selection = mysql_select_db("md_update", $link); $filename = $upfile; $id = fopen($filename, "r"); while ($data = fgetcsv($id, filesize($filename))) $table[] = $data; fclose($id); foreach($table as $row) { $sql ="UPDATE md_update SET id='".$row['0']."',name='".$row['1']."',member_no='".$row ['2']."',tradingname='".$row['3']."',street='".$row['4']."',town='".$row['5']."',county='".$row ['6']."',post_code='".$row['7']."',phone='".$row['8']."',board_area='".$row['9']."',username='".$row ['10']."',password='".$row['11']."',validity='".$row['12']."',type_desc='".$row['13']."' WHERE id='".$row['0']."'"; @mysql_query($sql, $connection) or die(mysql_error()); } unlink($upfile); } } Where abouts would I put an "else" statement? So if there is any new data in the CSV file, then it will "INSERT" the new data. Any help would be great Thanks in advance Ed Link to comment https://forums.phpfreaks.com/topic/51982-updating-database/ Share on other sites More sharing options...
Barand Posted May 18, 2007 Share Posted May 18, 2007 Have a look at MySQL REPLACE statements. Like INSERT but updates if record exists and insert if it does not. Link to comment https://forums.phpfreaks.com/topic/51982-updating-database/#findComment-256239 Share on other sites More sharing options...
Canman2005 Posted May 18, 2007 Author Share Posted May 18, 2007 will do, thanks Link to comment https://forums.phpfreaks.com/topic/51982-updating-database/#findComment-256248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.