[英]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. 我知道我与数据库的连接有效,并且我没有使用自动增量ID进行的测试对我来说效果很好。 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.
我的表有3列,ID(自动递增),名称和值。 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. 不建议使用mysqli_bind_param,它只是mysqli_stmt_bind_param的别名,mysqli_stmt_bind_param希望绑定类型是字符串而不是数组。 so just change that line.
所以只要改变那条线。
mysqli_stmt_bind_param($stmt, 'si', $name, $value);
I'd recommend you using PDO ( http://ar2.php.net/PDO ). 我建议您使用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 ;)
它是用于不同数据库的面向对象的界面,语法更简洁,使您从RDBMS中抽象出来;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.