Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    error with sql code

    i am getting an error when i try to make this sql code
    i cant seem to figure out how to solve
    [CODE]
    foreach($items as $value)
    {
    echo $value['id']; //this works

    $query = "INSERT INTO products (products_id, Id,products_price, products_quantity, products_weight,products_length,products_width,products_height,products_image, vendors_id,products_status,products_date_added)Values ('NULL', $value['id'], $value['price'], $value['quantity'], $value['weight'], $value['length'], $value['width'], $value['height'], $valu['thumbpicurl'], $value['vendor'], '1', 'NULL') ";
    }
    [CODE]
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    $query "INSERT INTO products (products_id, Id,products_price, products_quantity, 
    products_weight,products_length,products_width,products_height,products_image, 
    vendors_id,products_status,products_date_added)Values ('NULL', {$value['id']}, {$value['price']}, 
    {$value['quantity']}, {$value['weight']}, {$value['length']}, {$value['width']}, {$value['height']}, 
    {$valu['thumbpicurl']}, {$value['vendor']}, '1', 'NULL') "

    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Does not concern the current problem so just in case: If you are inserting NULL, not string 'NULL', please do not surround it with single quotes. With your query you are inserting strings "NULL" into the DB, not NULL values.

    Also please notice that (unless you have not shown this code) all string values must be escaped with mysql_real_escape_string() (I have supposed you are using MySQL) to protect it against SQL injection attacks. Integer and floating point values must be cast explicitly to the corresponding types with intval() or floatval() correspondingly.

    And if you have strings, please surround them with single quotes. E.g. {$value['thumbpicurl']} would most probably be a string. If yes, it should be
    '{$value['thumbpicurl']}'
    instead.


  •  

    Posting Permissions

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