Tuesday, 15 September 2015

php - mySQL Insert if id exist else send error to user, what metod can I use? -



php - mySQL Insert if id exist else send error to user, what metod can I use? -

i alter code bit , if int $itemid not exist user should trigger $retval die, if $itemid exists user should proceed update of db.

what method suitable?

code:

$upd = "update booking set userid ='$userid' itemid ='$itemid'"; $retval = mysql_query($upd, $con); if(!$retval){ die('could not update data: '.mysql_error()); } echo "updated info successfully\n";

you should check number of affected rows, this:

$upd = "update booking set userid ='$userid' itemid ='$itemid'"; $retval = mysql_query($upd, $con); if(!$retval || 0 === mysql_affected_rows()) { die('could not update data: '.mysql_error()); } echo "updated info successfully\n";

also, stated in comment, should using mysqli_ or pdo instead of mysql_, since latter outdated.

php mysql

No comments:

Post a Comment