2013-02-21 103 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