【问题标题】:You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use nea [duplicate]您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解使用 nea 的正确语法 [重复]
【发布时间】:2015-12-16 20:13:39
【问题描述】:

我收到此错误 //错误

ERRORINSERT INTO new_comp_reg (phno , fullname , address , dept , desc) VALUES ('','','','','') 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 'desc) VALUES ('','','' ,'','')' 附近使用正确的语法

PHP

<?php

    $servername = 'mysql.hostinger.in';
    $username = '';
    $password = '';
    $dbname = 'u424351292_icrcm';

    if(isset($_POST['submit']))
    {
        $phone_no = $_POST['phno'];
        $full_name = $_POST['fullname'];
        $location = $_POST['address'];
        $department = $_POST['dept'];
        $description = $_POST['desc'];
    }

        $conn = new mysqli($servername,$username,$password,$dbname);

        if($conn->connect_error)
        {
            die("Connection Failed" . $conn->connect_error);
        }

        $sql = "INSERT INTO new_comp_reg (phno , fullname , address , dept , desc)  VALUES ('$phone_no' , '$full_name' , '$location' , '$department' , '$description')";

        if($conn->query($sql) === TRUE)
        {
            echo "Complaint Registered";
        }
        else
        {
            echo "ERROR".$sql."<br>".$conn->error;
        }

    $conn->close();
    ?>

//错误

ERRORINSERT INTO new_comp_reg (phno , fullname , address , dept , desc) VALUES ('','','','','') 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 'desc) VALUES ('','','' ,'','')' 附近使用正确的语法

【问题讨论】:

  • Desc 是保留关键字,您应该转义它

标签: php mariadb


【解决方案1】:

desc 是一个reserved keyword in MySQL,需要用反引号转义。

INSERT INTO new_comp_reg (..., `desc`)  VALUES (...)

例如,或者将您的列名更改为 description

顺便说一句,您没有转义您的用户输入,这可能导致语法错误和 SQL 注入。使用准备好的语句。

【讨论】:

    【解决方案2】:
    if(isset($_POST['submit']))
    {
        $phone_no = $_POST['phno'];
        $full_name = $_POST['fullname'];
        $location = $_POST['address'];
        $department = $_POST['dept'];
        $description = $_POST['desc'];
    }
    
        $conn = new mysqli($servername,$username,$password,$dbname);
    
        if($conn->connect_error)
        {
            die("Connection Failed" . $conn->connect_error);
        }
    
        $sql = "INSERT INTO new_comp_reg VALUES ('$phone_no' , '$full_name' , '$location' , '$department' , '$description')";
    
        if($conn->query($sql) === TRUE)
        {
            echo "Complaint Registered";`enter code here`
        }
        else
        {
            echo "ERROR".$sql."<br>".$conn->error;
        }
    
    $conn->close();
    ?>
    

    【讨论】:

      【解决方案3】:

      我会说是

      $sql = "INSERT INTO new_comp_reg (phno , fullname , address , dept , desc)  VALUES ('".mysql_real_escape_string($phone_no)."' , '".mysql_real_escape_string($full_name)"' , '".mysql_real_escape_string($location)"' , '".mysql_real_escape_string($department)"' , '".mysql_real_escape_string($description)"')";
      

      这实际上会改善您的保护。还要检查你的列名,因为上面的悲伤可能是你引用了一个错误。

      【讨论】:

      • 这个答案不能解决问题,最重要的是建议使用已弃用的 API。 不要使用这个!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2018-11-27
      • 2018-11-28
      相关资源
      最近更新 更多