2013-02-21 96 views
-3

我嘗試值從表單輸入添加到MySQL數據庫的時候,它用好的工作,最近它開始給我這個錯誤:MySQL總是給錯誤插入到數據庫

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 'group) VALUES('כיריים על השיש','1','9')' at line 1.

我的代碼是:

if (isset($_POST['cat_name'])) 
{  
    $cat_name = mysql_real_escape_string($_POST['cat_name']); 
    $parent = mysql_real_escape_string($_POST['parent']); 
    $grp  = mysql_real_escape_string($_POST['grp']); 

    // See if that product name is an identical match to another product in the system 
    $sql   = mysql_query("SELECT id FROM categories WHERE cat_name='$cat_name' LIMIT 1"); 
    $productMatch = mysql_num_rows($sql); // count the output amount 

    if ($productMatch > 0) 
    { 
     echo 'Sorry you tried to place a duplicate "Category" into the system, <a href="category_add.php">click here</a>'; 
     exit(); 
    } 

    // Add this product into the database now 
    $sql2 = mysql_query("INSERT INTO categories (cat_name,parent,group) VALUES('$cat_name','$parent','$grp')") or die (mysql_error()); 
    $cid  = mysql_insert_id(); 
    $newname = "$cid.jpg"; 
    move_uploaded_file($_FILES['fileField']['tmp_name'], "../cat_images/$newname"); 
    header("location: category_add.php"); 
    exit(); 
+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo 2013-02-21 09:49:07

回答

3

使用此查詢,group是mysql中的保留字。您必須在列名稱後附加`

$sql2 = mysql_query("INSERT INTO categories (`cat_name`,`parent`,`group`) VALUES('$cat_name','$parent','$grp')") or die (mysql_error()); 
+1

工作!謝啦 ! – user2003332 2013-02-21 07:34:37

+1

你問了6個問題。沒有人接受。如果它對你有幫助,它就會接受答案的好習慣。 – 2013-02-21 07:36:46

+1

@ user2003332:一旦你滿意答案,接受它。 – 2013-02-21 07:39:31