简体   繁体   中英

MySQLi prepared update statement in PHP

How do you write a prepared update statement? Reference: mysqli::prepare

I've tried writing it as described:

  if ($stmt = $mysqli->prepare("UPDATE tblFacilityHrs SET title =? description = ? WHERE uid = ?")){
            $stmt->bind_param('sss', $title, $desc, $uid2);

            //Get params
            $title=$_POST['title'];
            $desc=$_POST['description'];
            $uid2=$_GET['uid'];     

$stmt->execute();
            $stmt->close();
    }
    else {
        //Error
        printf("Prep statment failed: %s\n", $mysqli->error);
    }

Error:

Prep statment failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description = ? WHERE uid = ?' at line 1 Edited row.

You're just missing a comma between the set columns:

UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?
                                ^^^^^^

When MySQL reports an error the likes of check the manual for syntax to use near 'something , look most often to the character immediately preceding the 'something , as that is where your error occurs.

$sql = "UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('sss', $title, $desc, $uid2);
$stmt->execute();

您可能需要添加逗号:

$stmt = $mysqli->prepare("UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?"

You are binding the parameters before assigning them to variables:

$title=$_POST['title'];
$desc=$_POST['description'];
$uid2=$_GET['uid']; 

$stmt->bind_param('sss', $title, $desc, $uid2);

edit : scratch that, it doesn't appear to make a difference whether or not the parameters are bound before or after you have defined the variables (you learn something new everyday!), but like Michael said, logically it makes sense to define them first.

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