吴环宇 吴环宇 - 3 months ago 8
PHP Question

PHP prepared statement cannot work.And I don't know what is going wrong

Code below here cannot work.And I don't know where the problem is:

$insert_str="insert into User(Name,Password)values(?,?)";
$stmt->$conn->prepare($insert_str);
$stmt->bind_param("ss",$name,$password);
$stmt->execute();
$stmt->close();


I can gurantee that mysql connection and mysql configuration are ok.Because code like this works:

$insert_str="insert into User(Name,Password)values('$name','$password')";
$conn->query($insert_str);
echo "Create Account Successfully";


Could someone help please? I have being considering potential problems for over 2 hours but nothing changed.

Answer

You need to assign the variable $stmt as the return from the prepare statement, like this:

 $insert_str="insert into User(Name,Password)values(?,?)";
 $stmt=$conn->prepare($insert_str);
 $stmt->bind_param("ss",$name,$password);
 $stmt->execute();
 $stmt->close();
Comments