SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding data to database problem

    Hi,

    I am using

    PHP Code:
    function add_content($p) {
            
    $productid mysql_real_escape_string($p['productid']);
            
    $categoryid mysql_real_escape_string($p['categoryid']);
            
    $name mysql_real_escape_string($p['name']);
            
    $order mysql_real_escape_string($p['order']);
            
    $price mysql_real_escape_string($p['price']);
        
        
    $sql "INSERT INTO products (id, productid, categoryid, name, order) VALUES (null, '$productid', '$categoryid', '$name', '$order')";
        
    $res mysql_query($sql) or die(mysql_error());
        
        }

    The problem is, as you can see I am adding:
    id
    productid
    categoryid
    name
    order

    The error I get is:

    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 'order) VALUES (null, '1', '2', '3', '4')' at line 1

    If I only add:
    id
    productid
    categoryid
    name

    (ie. not including order)

    then it works.

    Also if I add 'price' instead of 'order' it works.

    So it seems to me the problem is with the field 'order' but I am adding this field like the others I have done.

    May be you know why I get the error above when adding this field???

    Matt

  2. #2
    SitePoint Guru r2d2's Avatar
    Join Date
    Dec 2003
    Location
    In my van, fool!
    Posts
    646
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to put ` marks around `order` because it is a reserved keyword in MySQL. Its a backtick - top left of the keyboard, next to 1. eg:

    PHP Code:
        $sql "INSERT INTO products (id, productid, categoryid, name, `order`) VALUES (null, '$productid', '$categoryid', '$name', '$order')"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    order is a reserved word in mysql
    change it in something else (orderid for example) or put backticks around it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •