Forum Moderators: coopster

Message Too Old, No Replies

From Insert to Update

         

topherknowles

9:46 am on Jul 12, 2010 (gmt 0)

10+ Year Member



Hello, could anybody help me turn the following statement:

for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode(",", $line);


$sql = "insert into tblHolidays values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}

...into an update query? It's scanning my csv file correctly initially and inserting the rows, but when I come to use it a second time I want to update the existing rows with any new data.

Can't seem to get the right syntax... or maybe I'm going about it all wrong, either way I'm tearing my hair out!

lammert

9:57 am on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you need to do is define a unique index on the destination table. Then your query must be modified to do an update if the insert fails with a duplicate entry error. MySQL uses the ON DUPLICATE KEY construction for this.