简体   繁体   中英

Can't Insert Data Into Tables Containing Auto Increment Primary Key Using PHP Prepared Statements

I know I have that my connection to the database works, and a test I did using no auto-increment id worked fine for me. The code below refuses to work and I can't find aa way around it. My table has 3 columns, ID (auto increment), name and value. What do I need to change in order to get this to work?

Thanks in advance

//create placeholder query
 $query = "INSERT INTO prepared_test (name, value) VALUES (?,?)";

 //prepare the query
 $stmt = mysqli_prepare($connection, $query);

 $name = 'steve';
 $value = 45;

 mysqli_bind_param($stmt, array(MYSQLI_BIND_STRING, MYSQLI_BIND_INT), $name, $value);
 mysqli_execute($stmt);

 if(mysqli_stmt_affected_rows($stmt) != 1)
  die("issues");

 mysqli_stmt_close($stmt);

 $connection->close();

mysqli_bind_param is deprecated and is just an alias of mysqli_stmt_bind_param, mysqli_stmt_bind_param expects the bind types to be a string not an array. so just change that line.

mysqli_stmt_bind_param($stmt, 'si', $name, $value);

I'd recommend you using PDO ( http://ar2.php.net/PDO ). It's an object oriented interface for different databases, it has a cleaner syntax and abstracts you from the RDBMS ;)

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