插入数据库时​​,Mysql一直出错

时间:2013-02-21 07:29:13

标签: mysql php

我尝试从表单输入向MySQL数据库添加值,并且它曾经工作正常,最近它开始给我这个错误:

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行的“组”VALUES('כירייםעלהשיש','1','9')附近使用正确的语法。

我的代码是:

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();

1 个答案:

答案 0 :(得分:3)

使用此查询,group是mysql中的保留字。您必须使用列名称附加`

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