getting a mysql error...

Discussion in 'PHP' started by Greenmethod, Aug 9, 2007.

  1. #1
    I am trying to insert some data into a database, and am getting
    "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 'Pawn_Num='1', Pawn_Port='LW3', Pawn_Path='\\\\PS1\\LPT1', Pawn_Mod='', Rec_Num='' at line 1"

    My code is as follows:

    
    mysql_query("INSERT INTO printers SET cust_no='$cust_no' Pawn_Num='$Pawn_Num', Pawn_Port='$Pawn_Port', Pawn_Path='$Pawn_Path', Pawn_Mod='$Pawn_Mod', Rec_Num='$Rec_Num', Rec_Port='$Rec_Port', Rec_Path='$Rec_Path', Rec_Mod='$Rec_Mod', Rep_Num='$Rep_Num', Rep_Port='$Rep_Port', Rep_Path='$Rep_Path', Rep_Mod='$Rep_Mod', Lab_Num='$Lab_Num', Lab_Port='$Lab_Port', Lab_Path='$Lab_Path', Lab_Mod='$Lab_Mod', Jew_Num='$Jew_Num', Jew_Port='$Jew_Port', Jew_Path='$Jew_Path', Jew_Mod='$Jew_Mod'") or die(mysql_error());
    PHP:
    does anyone have any idea what i'm doing wrong here? Thanks in advance!
     
    Greenmethod, Aug 9, 2007 IP
  2. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Hi,

    yes coz youre trying to insert a data, not updating or vice versa. Check your SQL statement

    example insert:

    INSERT INTO mytable(myfield1) VALUES('myvalue')

    example update

    UPDATE mytable SET myfield1='myvalue'

    Thanks,

    Mike

     
    gigamike, Aug 9, 2007 IP
  3. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You're missing a comma between....

    cust_no='$cust_no' and Pawn_Num='$Pawn_Num'

    cust_no='$cust_no', Pawn_Num='$Pawn_Num'
     
    ecentricNick, Aug 9, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Mike, you can perform inserts that way. See 2nd example here...

    http://dev.mysql.com/doc/refman/4.1/en/insert.html

    
    INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
        [INTO] tbl_name [(col_name,...)]
        VALUES ({expr | DEFAULT},...),(...),...
        [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
    Or: 
    INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
        [INTO] tbl_name
        SET col_name={expr | DEFAULT}, ...
        [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
    
    Code (markup):
    The problem is a missing comma in the guy's statement. See my other post on this thread.
     
    ecentricNick, Aug 9, 2007 IP
  5. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    But I am adding a new row to the database. I do want to use INSERT, don't I? Or are you just saying that I need to put column1,column2,column3, VALUES data1,data2,data3? I'm a little confused because from what I've read I think you should be able to do it the same way as an update. eg.. column1='data1',column2='data2' and so on. Thanks for your help.
     
    Greenmethod, Aug 9, 2007 IP
  6. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That's what I thought.. any other reason that I would be getting that error?
     
    Greenmethod, Aug 9, 2007 IP
  7. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No,l Greenmethod. your syntax is acceptable in MySql - though it's not standard SQL which would use columns VALUES data format.

    Your problem is simply a missing comma.
     
    ecentricNick, Aug 9, 2007 IP
  8. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yes, see my other posts in your thread about the comma you've missed in your statement!
     
    ecentricNick, Aug 9, 2007 IP
  9. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Hi ecentricNick

    Many thanks for the info.

    Mike

     
    gigamike, Aug 9, 2007 IP