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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts

    I have an error in my insert statement

    Please can anybody help me with this error? "SQLSTATE[42000]: Syntax error or access violation: 1064 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 'Usage,Quantity ,UnitCost ,Tax,Vat_5,UnitPrice ,Date_Log ,EntryTime,Reorder_Level' at line 1"

    Here is my code:

    <code>
    $con = new PDO($DSN, $USER, $PWD); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO head_inventory(Bar_Code ,Serial_Number ,Item_Name,Product_Name,Product_Model ,Cat_Name ,Item_Desc,Usage,Quantity ,UnitCost ,Tax,Vat_5,UnitPrice ,Date_Log ,EntryTime,Reorder_Level,Vendor,UserName)
    VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $stmt = $con->prepare($sql); $stmt->execute(array('" .$Bar_Code. "','".$Serial_Number."','".$Item_Name."','".$Product_Name."','".$Product_Model."','".$Cat_Name."','" .$Item_Desc."','".$Usage."','".$Quantity."','".$UnitCost."','".$Tax."','".$Vat_5."','".$UnitPrice."' ,'".$Date_Log."','".$EntryTime."','".$Reorder_Level."','".$Vendor."','".$UserName."'));
    </code>

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    try backsticks ` ` around the column names, (the key to the left of 1 on a british keyboard)
    PHP Code:
    $sql "INSERT INTO head_inventory(`Bar_Code` ,`Serial_Number` ,`Item_Name` ,`Product_Name` ,`Product_Model` ,`Cat_Name` ,`Item_Desc`,`Usage`,`Quantity` ,`UnitCost` ,`Tax` ,`Vat_5` ,`UnitPrice` ,`Date_Log ` ,`EntryTime` ,`Reorder_Level` ,`Vendor` ,`UserName`)
    VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, in particular the USAGE is a reserved word. That must be backticked in order to use it as a field identifier.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    also to prevent any errors of this type you might like to read this:

    http://en.wikipedia.org/wiki/Hungarian_notation


    ie prefix tables with TBL_ eg TBL_head_inventory
    and fields with FLD eg FLD_Bar_Code or FLD_Usage
    Last edited by Arcticwarrio; 02-25-2013 at 03:09 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

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