php - 只有第一个 Mysqli Insert 查询可以使用 PHP 工作,而其他查询则失败

标签 php mysql sql mysqli

我一直在将我的一个 Web 应用程序(该应用程序创建具有多个答案的测验)从使用 PostgreSQL 数据库转换为使用 MySQL 数据库。下面的查询以前在 PostgreSQL 中运行良好,但在 MySQL 中却无法运行。

第一个查询确实被正确插入,但是第二个和第三个查询根本没有插入。

然而奇怪的是,为两个失败的查询生成了一个序列 ID。我可以这么说,因为当我重试插入时,成功的第一个查询每次都会增加 3 个 ID 号。我还在每个查询之前使用 echo 来检查所有变量是否正确。另一件让我困惑的事情是我还在另一个表中插入一条记录,该表也工作正常,似乎多次插入到同一个表中但不起作用?

帮助克服这个困难将不胜感激!

//answer 1
$answercreatequery = mysqli_query($db_handle, "INSERT INTO answer (answerid, questionid, adescription, afilelocation, iscorrect) VALUES( default, '".$thisquestionid."', '".$adescription1."', '".$afilelocation."', '".$iscorrect1."') ");


//answer 2                  
$answercreatequery = mysqli_query($db_handle, "INSERT INTO answer (answerid, questionid, adescription, afilelocation, iscorrect) VALUES( default, '".$thisquestionid."', '".$adescription2."', '".$afilelocation."', '".$iscorrect2."') ");


//answer 3
$answercreatequery = mysqli_query($db_handle, "INSERT INTO answer (answerid, questionid, adescription, afilelocation, iscorrect) VALUES( default, '".$thisquestionid."', '".$adescription3."', '".$afilelocation."', '".$iscorrect3."') ");

最佳答案

您不需要为 mysql 中的 ids 传递“默认”。 mysql 会自动处理这个问题。只需从语句中省略 id 列和值即可。

此外,您对 SQL 注入(inject)持开放态度。

请参阅此处有关准备好的语句:

http://php.net/manual/en/mysqli.prepare.php

关于php - 只有第一个 Mysqli Insert 查询可以使用 PHP 工作,而其他查询则失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25104436/

相关文章:

php - 如何从数据库中获取在特定日期和时间段内未分配的用户列表

javascript - 在 20 个页面的网站的所有页面中包含一个通用的 html 标题(具有事件类)

mysql - Left Join with group by 跳过groupby

mysql 计算 sql 语法中的单词

php - 使用 Wordpress wp_query 搜索存储在数组中的多个变量的出现

php - MySQL 内连接/左连接

php - 选择 max 像 mysql - php

php - 如何在 PHP 中以 Mysql DateTime 格式打印当前月份

sql - Informix 7.3 中是否有用于测试字母或数字数据的内置函数?

mysql - 如何从单个表中选择当前日期和上一个日期?