1

嗨,我遇到了这个错误,我似乎看不到问题,所以任何想法,一套新的眼睛可能会有所帮助。

完全错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'desc='ittititi', price='22', img='img.png'' 附近使用正确的语法

    <?php
// Include MySQL class
require_once('../inc/mysql.php');
// Include database connection
require_once('../inc/global.inc.php');
// Include functions
require_once('../inc/functions.inc.php');
// Start the session
session_start();
?>
<?php

// try to create a new record from the submission
$genre = mysql_real_escape_string($_REQUEST['genre']);
$title =  mysql_real_escape_string($_REQUEST['title']);
$desc =  mysql_real_escape_string($_REQUEST['desc']);
$price =  mysql_real_escape_string($_REQUEST['price']);
$img= mysql_real_escape_string($_REQUEST['img']);

if (!empty($genre) && !empty($title) && !empty($desc) && !empty($price)  && !empty($img)) {

  // here we define the SQL command
  $query = "SELECT * FROM books WHERE title='$title'";

  // submit the query to the database
  $res=mysql_query($query);

  // make sure it worked!
  if (!$res) {
    mysql_error();
    exit;
  }

  // find out how many records we got
  $num = mysql_numrows($res);
  if ($num>0) {
    echo "<h3>That book title is already taken</h3>\n";
    exit;
  }

  // Create the record
  $query = "INSERT INTO books SET genre='$genre', title='$title', desc='$desc', price='$price', img='$img'";
  $res = mysql_query($query)or die(mysql_error());
  if (! $res) {
    echo mysql_error();
    exit;
  } else {
    echo "<h3>Book Created</h3>\n";
    echo $_SESSION['title']=$title;
  }
}
?>


<form name="newbook" method="post">
<table border=0>
<tr> 
  <td>Genre:</td>
  <td><input type=text name='genre'></td>
</tr>

<tr> 
  <td>Title:</td>
  <td><input type=text name='title'></td>
</tr>

<tr> 
  <td>Description:</td>
  <td><input type=text name='desc'></td>
</tr>

<tr> 
  <td>Price:</td>
  <td><input type=number name='price'></td>
</tr>
<tr> 
  <td>Image:</td>
  <td><input type=text name='img'></td>
</tr>

    <tr> 
      <td colspan=2>
       <input type=submit value="Create my account">
      </td>
    </tr>
    </table>
    </form>
4

3 回答 3

4

您需要像反引号一样转义MySQL 中的保留字desc

INSERT INTO books 
SET genre = '$genre', title = '$title', `desc` = '$desc'
                                        ^----^-----------------here
于 2013-04-29T16:32:54.967 回答
1

desc 是 mysql 的保留关键字

像这样使用它

     `desc`

这必须符合您的查询

 $query = "INSERT INTO books SET genre='$genre', title='$title', `desc`='$desc', price='$price', img='$img'";
于 2013-04-29T16:32:39.647 回答
1

不要desc用作列名;它是一个关键字。如果将其用作列名,则必须引用它。

于 2013-04-29T16:32:48.563 回答