语法错误mySQL从PHP表单添加到DB

时间:2014-08-10 09:07:37

标签: php mysql sql forms insert

所以我试图让表单将数据添加到mySQL表中。我有这个表格

<form name="addBook" action="addBook.php" method="post" >
ISBN: <input type="text" name="isbn"><br />
Name: <input type="text" name="name"><br />
Edition: <input type="text" name="edition"><br />
Author: <input type="text" name="author"><br />
Class: <input type="text" name="class"><br />
Department: <input type="text" name="department"><br />
Condition: <input type="text" name="condition"><br /><br />
<input type="submit" value="Add Book">
</form>

addBook.php在哪里......

<?php
$con=mysqli_connect("cclloyd.com","cclloyd","","Inventory");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$isbn = mysqli_real_escape_string($con, $_POST['isbn']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$edition = mysqli_real_escape_string($con, $_POST['edition']);
$author = mysqli_real_escape_string($con, $_POST['author']);
$class = mysqli_real_escape_string($con, $_POST['class']);
$department = mysqli_real_escape_string($con, $_POST['department']);
$condition = mysqli_real_escape_string($con, $_POST['condition']);

$sql="INSERT INTO Books (isbn, name, edition, author, class, department, condition)
VALUES ('$isbn', '$name', '$edition', '$author', '$class', '$department', '$condition')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

mysqli_close($con);

header('Location: http://umassd.cclloyd.com/bookadded.php' ) ; 
?>

当我执行它时,我收到了这个错误。 &#34;错误:您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便使用“条件”(&#39; l&#39;,&#39; lk&#39;,&#39; l&#39; ,&#39; k&#39;,&#39; j&#39;,&#39; h&#39;,&#39; h&#39;)&#39;在第1行&#34;

那些只是随机的东西,我填写表格。错误在哪里?我在网上看了很多,他们都说要像我一样输入它。

2 个答案:

答案 0 :(得分:2)

condition是Mysql的保留字。检查保留字here

将单词放在引号中。

答案 1 :(得分:0)

请使用此

 $sql="INSERT INTO Books (`isbn`, `name`, `edition`, `author`, `class`, `department`, `condition`)
VALUES ('$isbn', '$name', '$edition', '$author', '$class', '$department', '$condition')";