2015-01-22 5 views
-1

그냥 빨리 일이, 내가 특정 문자열을 검색하는 것을 시도하고있다 , 그것이 내가이 코드를 실행하면 I 데이터베이스이상한 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 

구문에서 실수 한 적이 있습니까? 나는 많은 것을 시도했지만 그것이 될 수있는 것이 단서입니다. - 같은 특수 문자가 포함 된

감사

+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 서버 구문이다. 질문은 MySQL 태그입니다. –