2016-03-09 59 views
0

使用此查詢時,出現錯誤「您的SQL語法中有錯誤;請檢查與您的MySQL服務器版本相對應的手冊, 「向MySql插入新記錄時的SQL語法錯誤

$sql = 'INSERT INTO articles ( long_text) 
     VALUES ("' . mysqli_real_escape_string($conn, $long) . '")'; 

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

有誰知道是什麼原因造成的問題?

+2

添加完整的錯誤消息。瞭解準備好的語句 – Jens

+0

在$ sql變量'echo $ sql'後面添加;並向我們​​展示結果 – LightNight

+1

您將在雙引號(「)引用的查詢中插入文本,而在SQL文本中應該用單引號(')進行索引。 – Renzo

回答

1

查詢中的報價問題。你正打算在單引號內寫雙引號。

你需要改變你的查詢作爲prepare statement作爲

$stmt = $conn->prepare("INSERT INTO articles (`long_text`) VALUES (?)"); 
$stmt->bind_param('s', $long); 
/* execute prepared statement */ 
$stmt->execute(); 

printf("%d Row inserted.\n", $stmt->affected_rows); 

/* close statement and connection */ 
$stmt->close(); 

http://php.net/manual/en/mysqli-stmt.bind-param.php