dousi1906 2014-06-18 11:59
浏览 46
已采纳

mySQL如果id存在则插入其他发送错误给用户,我可以使用哪个metod?

I like to change my code up a bit and if the int $itemID does not exist the user should trigger $retval die, if $itemID exists the user should proceed with the update of the db.

What method is most suitable?

code:

$upd = "UPDATE booking SET userID ='$userID' WHERE itemID ='$itemID'";

$retval = mysql_query($upd, $con);
if(!$retval){
   die('Could not update data: '.mysql_error());
   } 
echo "Updated data successfully
";
  • 写回答

6条回答 默认 最新

  • dss67853 2014-06-18 12:04
    关注

    You should check for the number of affected rows, like this:

    $upd = "UPDATE booking SET userID ='$userID' WHERE itemID ='$itemID'";
    
    $retval = mysql_query($upd, $con);
    
    if(!$retval || 0 === mysql_affected_rows()) {
       die('Could not update data: '.mysql_error());
    }
    
    echo "Updated data successfully
    ";
    

    Also, as stated in the comment, you should be using mysqli_ or even PDO instead of mysql_, since the latter is seriously outdated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭