简体   繁体   English

无法使用PHP准备的语句将数据插入包含自动增量主键的表中

[英]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.

相关问题 MySQL具有1个主键,可自动递增2个表 - MySQL having 1 primary key with auto increment for 2 tables 如何从具有主键自动增量的表中向具有外键的表中插入数据? - How can I insert data to a table which has a foreign key, from a table which has a primary key auto-increment? 使用自动增量键插入多个表 - Insert into multiple tables using auto-increment key 无法使用预准备语句插入mySQLi DB - Can't Insert into mySQLi DB using Prepared Statements 在PHP中使用准备好的语句使用外键插入/显示多个MySQL表 - Use prepared statements in PHP to insert into/display from multiple MySQL tables with foreign key 为什么我不能使用 PHP 准备语句从 SQL 行获取数据? - Why can't I get data from SQL row using PHP prepared statements? 如何使用PHP中的准备好的语句在多个表中插入数据? - How to insert data in multiple tables using a prepared statement in PHP? PHP尝试使用准备好的语句将数据插入两个不同的表中 - PHP Trying to insert data into two different tables using prepared statment PHP mySQL - 在主键上使用自动递增将新记录插入表中 - PHP mySQL - Insert new record into table with auto-increment on primary key 我无法使用准备好的语句将数据输入到表中 - I can't input the data into my table using prepared statements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM