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


Only first Mysqli Insert query with PHP working while others fail

我一直在转换我的一个web应用程序,它创建了一个从PostgreSQL DB到MySQL DB的多个答案的测验。下面的查询以前在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中为id传递"default"。Mysql会自动处理。只需从语句中省略id列和值。

同时,你对SQL注入敞开了大门。

查看这里的预处理语句:

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

相关文章: