2013-04-29 65 views
1

嗨即時通訊運行到這個錯誤,我似乎不能看到問題,所以任何想法,新的眼睛可能會幫助。你的SQL語法錯誤

完整錯誤:

您的SQL語法錯誤;檢查對應於你的MySQL服務器版本的手冊正確的語法使用近「DESC =」 ittititi「價格= '22' ,IMG =‘img.png’在行」 1

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

你需要逃避reserved words in MySQLdesc與反引號

INSERT INTO books 
SET genre = '$genre', title = '$title', `desc` = '$desc' 
             ^----^-----------------here 
1

遞減是爲MySQL保留關鍵字

使用它像

 `desc` 

這必須您甲腎上腺素查詢

$query = "INSERT INTO books SET genre='$genre', title='$title', `desc`='$desc', price='$price', img='$img'"; 
1

不要使用desc作爲列名;它是一個關鍵字。如果您將它用作列名稱,則必須引用它。

相關問題