MySQL update after the mysql_affected_rows to determine whether the success, no insert, the result of a problem, the number of table rows multiplied, the code is as follows, solve
Data written to database function save_db ($currencyname _en, $currencyname _cn, $buyingrate, $sellingrate, $middlerate) {//update $query _update = ' Update '. TABLENAME. ' SET '. ' buyingrate= '. $buyingrate ', '. ' sellingrate= '. $sellingrate. ', ' . ' Middlerate= '. $middlerate. ', '. ' Lastupdatetime= '. Date (' y-m-d h:i:s ', Time ( )) .'"' .' WHERE currencyname_en= "'. $currencyname _en. '"; mysql_query ($query _update); if (Mysql_affected_rows ()!=1)//update unsuccessful, try inserting {$query _insert = ' insert INTO '. TABLENAME. ' (Currencyname_en, CURRENCYNAME_CN, Buyingrate, Sellingrate, Middlerate, LastUpdateTime) VALUES ( "'. $currencyname _en. '", "'. $currencyname _ CN. ' "," '. $buyingrate. ' ", "'. $sellingrate. '", "'. $middlerate. '", "'. Date (' y-m-d h:i:s ', Time ()). '" )'; mysql_query ($query _insert); } if (Mysql_affected_rows ()!=1) {$err. = "
Insert or update ". $currencyname _en." -". $key." CURRENCYNAME_CN "; } }
Reply content:
MySQL update after the mysql_affected_rows to determine whether the success, no insert, the result of a problem, the number of table rows multiplied, the code is as follows, solve
Data written to database function save_db ($currencyname _en, $currencyname _cn, $buyingrate, $sellingrate, $middlerate) {//update $query _update = ' Update '. TABLENAME. ' SET '. ' buyingrate= '. $buyingrate ', '. ' sellingrate= '. $sellingrate. ', ' . ' Middlerate= '. $middlerate. ', '. ' Lastupdatetime= '. Date (' y-m-d h:i:s ', Time ( )) .'"' .' WHERE currencyname_en= "'. $currencyname _en. '"; mysql_query ($query _update); if (Mysql_affected_rows ()!=1)//update unsuccessful, try inserting {$query _insert = ' insert INTO '. TABLENAME. ' (Currencyname_en, CURRENCYNAME_CN, Buyingrate, Sellingrate, Middlerate, LastUpdateTime) VALUES ( "'. $currencyname _en. '", "'. $currencyname _ CN. ' "," '. $buyingrate. ' ", "'. $sellingrate. '", "'. $middlerate. '", "'. Date (' y-m-d h:i:s ', Time ()). '" )'; mysql_query ($query _insert); } if (Mysql_affected_rows ()!=1) {$err. = "
Insert or update ". $currencyname _en." -". $key." CURRENCYNAME_CN "; } }
INSERT INTO table(...)VALUES(...)ON DUPLICATE KEY UPDATE field1 = ?, field2 = ?, ...
Wouldn't that be a little simpler?