无法使用 PHP 准备语句将数据插入到包含自增主键的表中
我知道我与数据库的连接正常,并且我使用无自动增量 id 进行的测试对我来说效果很好。下面的代码无法工作,我找不到解决方法。我的表有 3 列,ID(自动增量)、名称和值。 为了让它发挥作用,我需要改变什么?
提前致谢
//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();
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 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mysqli_bind_param 已弃用,它只是 mysqli_stmt_bind_param 的别名,mysqli_stmt_bind_param 期望绑定类型是字符串而不是数组。所以只需更改该行即可。
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.
我建议您使用 PDO (https://www.php.net/PDO)。它是一个面向不同数据库的面向对象接口,它具有更清晰的语法,并将您从 RDBMS 中抽象出来;)
I'd recommend you using PDO (https://www.php.net/PDO). It's an object oriented interface for different databases, it has a cleaner syntax and abstracts you from the RDBMS ;)