简体   繁体   中英

prepared statements don't work

if(isset($_SESSION['id'])) {
     $message=$_POST['message'];
     $cid=$_POST['cid'];
     $user_id=$_SESSION['id'];
     $stmt=$conn->prepare("update comments set message=? where id=? and user_id=?");
     $stmt->bind_param("sss",$message,$cid,$user_id);
     if(!$stmt->execute()){
          echo "error";
     } else {
          echo "success";
     }

I always get success but when I go to the database I find out that nothing is changed.

You are trying to do an UPDATE with a WHERE clause. If it does not actually update anything, it will still be considered a success. The reason !$stmt->execute() would happen, is if there is a sql error, and it bails.

So what you should be doing, is also check if num rows affected.

if ( ! $stmt->affected_rows ) { echo 'nothing changed!'; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM