MySQLi has prepared update instructions in PHP

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

I tried to write 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:

The preliminary step failed: you encountered an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use near 'description =? WHERE uid =? 'on line 1 Edited line.

+4
source share
3 answers

You just do not have enough comma between installed columns:

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

When MySQL reports an error, for example, checking the syntax guide for the use of "something", it is most often to look at the character immediately preceding "something", since that is where your error occurs.

Note: you may need to call bind_param() after setting the input variables, not earlier. I don’t remember how MySQLi parses them and when they are connected, but it makes sense logically in the code to install them first and then bind them.

 //Get params $title=$_POST['title']; $desc=$_POST['description']; $uid2=$_GET['uid']; $stmt->bind_param('sss', $title, $desc, $uid2); 
+14
source

You probably need to add commas:

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

You bind the parameters before assigning them variables:

 $title=$_POST['title']; $desc=$_POST['description']; $uid2=$_GET['uid']; $stmt->bind_param('sss', $title, $desc, $uid2); 

edit : scratch, which doesn't seem to matter if the parameters are related before or after you define the variables (you learn something new every day!), but Michael said it makes sense to define them first .

+1
source

Source: https://habr.com/ru/post/1412833/


All Articles