繁体   English   中英

查询失败:您的SQL语法有误;

[英]QUERY FAILED:You have an error in your SQL syntax;

当我在文本中使用(')时出现错误

如何使用mysqli_real_escape_string(); 这个代码?

查询失败:您的SQL语法有误; 检查与您的MariaDB服务器版本相对应的手册,以在附近使用正确的语法


<?php

                    if(isset($_POST["add_post"])){
                        $post_title = $_POST["post_title"];
                        $post_category = $_POST["post_category"];
                        $post_tags = $_POST["post_tags"];
                        $post_text = $_POST["post_text"];
                        $post_date = date("d/m/y");

                        $post_image = $_FILES["post_image"]["name"];
                        $post_image_temp = $_FILES["post_image"]["tmp_name"];

                        move_uploaded_file($post_image_temp, "../images/$post_image");

                        $query = "INSERT INTO posts (post_title, post_category, post_text, post_tags, post_date, post_image)";

                        $query .= "VALUES('$post_title', '$post_category', '$post_text', '$post_tags', now(), '$post_image')";

                        $create_post_query = mysqli_query($conn, $query) ;

                        if(!$create_post_query) {
                            die("QUERY FAILED:" .mysqli_error($conn));
                        } else {

                        header("Location: posts.php");

                    }
                    }

                    ?>

您从POST输入原始值。 不要这样做。 至少有两个原因:

  1. 一些值可能包含'符号,这会导致sql语法错误。 您应该转义所有在sql查询中串联的值。
  2. 这种方法导致sql注入(一种黑客攻击)。

我建议使用PDO代替或至少使用mysql-real-escape-string函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM