2015-01-22 2 views
-1

Просто быстро вещь, Я пытаюсь искать для конкретной строки, все это находит мне нужно вставить в базу данныхСтранно MySQL ошибка синтаксиса вставки

Когда я запускаю этот код:

<?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 

ли я сделать misstake в моем синтаксисе? Я много пробовал, но я не знаю, что это может быть.

Благодаря

+0

Вы должны вас кавычка вокруг имени таблицы из-за '-'. – Goro

+0

Вот почему вы никогда не должны использовать такие символы, как '-' в именах таблиц. Вместо этого используйте подчеркивание. – Gogol

+0

Также вы должны переместить создание соединения до цикла while. (и закройте его после) – Hacketo

ответ

4

столбцов или таблиц имена, содержащих специальные символы, таким как - должны быть экранированы с обратными кавычками. Используйте

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

В противном случае MySQL интерпретирует это как 'ingame' MINUS' banlist' ;-) –

0

Обновления Добавленного запрос и попробуйте, как это -

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

Это синтаксис SQL-Server. Вопрос помечен как MySQL. –