【问题标题】:MySQL error - “You have an error in your SQL syntax" [duplicate]MySQL 错误 - “您的 SQL 语法有错误” [重复]
【发布时间】:2016-12-30 10:21:54
【问题描述】:

我的代码是:

if (isset($_POST['add'])) {
    $query = "INSERT INTO ".$dbPrefix."posts (`id`, `user`, `name`, `link`, `content`, `date`, `private`, `password`) VALUES (NULL, '$user', '$name', '$link', '$content', '$date', '$private', '$pass');";
    $mysqli->query($query) OR $status = 'Oprostite, pri dodajanju je prišlo do težave.';
    $id=$mysqli->insert_id;
    foreach ($_POST['categories'] as $category) {
        $categoryQuery.="INSERT INTO ".$dbPrefix."category_posts (`categoryID`, `postID`) VALUES ('".$category."','".$id."');";
    }
    $mysqli->query($categoryQuery) OR $status = $mysqli->error;
}
echo $status;

我的变量 $_POST 是:

大批 ( [名称] => (((((((((()) [描述] =>)))))) [关键词] => [类别] => 数组 ( [0] => 1 [1] => 2 [2] => 3 ) [私人] => 0 [密码] => [日期] => 0 [日期时间] => [添加] => Dodaj )

如果我打印查询并在phpMyAdmin中运行它没有问题,否则会显示错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“INSERT INTO fc_category_posts (`categoryID`, `postID`) VALUES ('2','116');INSERT' 附近使用正确的语法

查询是:

INSERT INTO fc_category_posts (`categoryID`, `postID`) VALUES ('1','116');
INSERT INTO fc_category_posts (`categoryID`, `postID`) VALUES ('2','116');
INSERT INTO fc_category_posts (`categoryID`, `postID`) VALUES ('3','116');

【问题讨论】:

  • 好像你一次只能通过一个INSERT。
  • 如何自定义可以工作的代码
  • 您只需将循环代码更改为:foreach ($_POST['categories'] as $category) { $categoryQuery ="INSERT INTO ".$dbPrefix."category_posts (categoryID, postID) VALUES ('".$category."','".$id."');"; $mysqli->query($categoryQuery);}
  • 您不能在单个 query() 调用中运行多个查询语句,这是针对一种形式的 sql injection attack 的基本防御机制,您的代码完全容易受到这种攻击。

标签: php mysql sql


【解决方案1】:

如果你只能通过INSERT那为什么不这样做;

INSERT INTO fc_category_posts (`categoryID`, `postID`) 
VALUES ('1','116'),('2','116'),('3','116');

节省您运行多个 INSERT INTO 语句的时间。

【讨论】:

  • 在这种情况下将查询移动到循环内会更简单,而不是重写代码来完成这项工作
  • 可能,我是一个简单的 SQL 人,所以以这种方式寻找答案。
【解决方案2】:

只需简单地将查询的执行移到循环内

if (isset($_POST['add'])) {
    $query = "INSERT INTO ".$dbPrefix."posts 
                   (`id`, `user`, `name`, `link`, `content`, `date`, 
                    `private`, `password`) 
               VALUES (NULL, '$user', '$name', '$link', '$content', '$date', 
                       '$private', '$pass');";

    $mysqli->query($query) OR $status = 'Oprostite, pri dodajanju je prišlo do težave.';
    $id=$mysqli->insert_id;
    foreach ($_POST['categories'] as $category) {

        //Note I have chnages `.=` to `=` in this statement
        $categoryQuery = "INSERT INTO ".$dbPrefix."category_posts 
                                   (`categoryID`, `postID`) 
                            VALUES ('$category','$id');";

        $mysqli->query($categoryQuery) OR $status = $mysqli->error;
    }

}
echo $status;

但是您的脚本存在SQL Injection Attack 的风险 看看Little Bobby TablesEven 发生了什么 if you are escaping inputs, its not safe! 使用prepared parameterized statements

在这个场景中,考虑在事务中运行它们也很有用,这样如果一个插入失败,你就不会让 darabase 变得一团糟The manual for mysqli::begin_transaction

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2012-10-10
    • 1970-01-01
    • 2013-02-01
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多