奇怪的MySQL插入语法错误

时间:2015-01-22 05:41:31

标签: php mysql mysqli

快一点, 我正在尝试搜索特定的字符串,它发现我需要插入数据库的所有内容

当我运行此代码时:

  <?php
    $search = 'permanently'; 
        $logfile = 'ban_list.txt';
        // Read from file 
        $file = fopen($logfile, "r");
        while( ($line =  fgets($file) )!= false)
        {
        if(stristr($line,$search))   // case insensitive
            echo "<font face='Arial'> $line </font><hr>";
            ?>
            <?php   
            $servername = "localhost";
            $username = "root";
            $password = "pass";
            $dbname = "db";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 

            $sql = "INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('$line', '$timestamp')";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }

            $conn->close();
        }
        fclose($file);
    ?>

我收到此错误:

118441 # reichskommisar is banned permanently by A_GismoDyret.
Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1 1213174 # SwagFurMeinFuhrer is banned permanently by HA_Vincent.
Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1 1394124 # Kick_Ass is banned permanently by HA_Vincent.
Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1    Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1    Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1    Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1    Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1    Error: INSERT INTO ingame-banlist (Ban, Timestamp) VALUES ('', '0')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-banlist (Ban, Timestamp) VALUES ('', '0')' at line 1 1762050 # VladimirKozlov is banned permanently by SO_Conner. # do not unban

我的语法错误了吗?我尝试了很多东西,但我无能为力。

由于

2 个答案:

答案 0 :(得分:4)

包含-等特殊字符的列名或表名必须使用反引号进行转义。使用

INSERT INTO `ingame-banlist` (Ban, Timestamp) VALUES ...

答案 1 :(得分:0)

更新您的插入查询并尝试这样 -

$sql = "INSERT INTO `ingame-banlist` (Ban, Timestamp) VALUES ('$line', '$timestamp')";