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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted May 18, 2007 Author Share Posted May 18, 2007 will do, thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.