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 refu开发者_StackOverflow社区ses to work and I can't find a a 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 ;)
精彩评论