只有第一个Mysqli插入查询与PHP工作而其他人失败

时间:2014-08-03 12:00:55

标签: php mysql sql mysqli

我一直在转换我的网络应用程序,它创建了一些测验,其中包括使用PostgreSQL数据库到MySQL数据库的多个答案。以下在PostgreSQL中运行良好的查询在MySQL中不起作用。

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

然而,奇怪的是,为两个失败的查询生成了一个序列号。我可以说,因为当我重试插入时,第一个成功的查询每次上升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."') ");

1 个答案:

答案 0 :(得分:2)

你不需要为mysql中的id传递“default”。 mysql自动处理。只需省略语句中的id列和值。

另外,你对sql注射很开放。

请参阅此处有关准备好的陈述:

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