【问题标题】:Only first Mysqli Insert query with PHP working while others fail只有第一个 Mysqli Insert 查询与 PHP 工作,而其他人失败
【发布时间】:2014-08-03 12:00:55
【问题描述】:

我一直在将我的一个 Web 应用程序转换为使用 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."') ");

【问题讨论】:

    标签: php mysql sql mysqli


    【解决方案1】:

    你不需要在 mysql 中为 ids 传递“默认值”。 mysql 会自动处理。只需从语句中省略 id 列和值即可。

    另外,你对 sql 注入非常开放。

    在这里查看准备好的语句:

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

    【讨论】:

    • 或者将其作为NULL 传递
    • @ydaetskcoR 嗯,我也不知道。早上 7 点,我已经学到了一些东西。
    • 完美,谢谢!此外,我在用户输入的所有数据上都使用了 mysqli_real_escape_string,如果不使用准备好的语句,我是否仍然可以打开?
    • @mark 这取决于你问谁和黑客的能力/时间。对付简单的机器人和扫描仪你会没事的,但最终你会遇到一个知道如何通过仔细编码的输入绕过它的黑客
    猜你喜欢
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多