MySQL Error in inserting data!

Discussion in 'PHP' started by sumitt_2004, Jun 17, 2009.

  1. #1
    Hi,

    I am facing a very strange problem in inserting data n MySQL through PHP. There are few fields in
    my form which are optional when someone leave optional fields blank
    then program encounter an error like this:

    Code is:
    $query_cust="INSERT INTO personal_details VALUES ($cust_id,'$cust_name','$cust_photo','$cust_post_area','$cust_per_add','$cust_force_batt',
    '$cust_irla','$cust_ppo','$cust_icard','$cust_rank','$dob','$cust_bank_name','$cust_bank_ac','$cust_bank_branch','$cust_pan','$cust_r_phone',
    '$cust_o_phone','$cust_mob','$cust_email','$cust_flat','$cust_title','$cust_rel','$cust_rel_name','$app_date')";

    Query is:
    INSERT INTO personal_details VALUES (3,'Sumit Kumar','','Only for Defense!','Ballabgarh','Force','','','','','1982-10-10','ICICI','025301529663','Sec 61, Noida','AUKPK539','','','9910888110','','Flat A - 600 Sq. Ft.','Mr.','S/o','Sh. Keshav Singh','2009-06-18')

    Invalid query: 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 ',,'','','')' at line 1

    Could someone please help me out in this?

    Regards,
    Sumit Kumar
     
    sumitt_2004, Jun 17, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    this is a bad practice for writing SQL statement yyou need to specific the fields in you insert statement
    e.g. insert into table (field1,field2) values ('1','2')
     
    javaongsan, Jun 17, 2009 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    does one of those variables contain a string with a ' in it?
     
    killaklown, Jun 17, 2009 IP
  4. sumitt_2004

    sumitt_2004 Peon

    Messages:
    154
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No, none of string contains ' but 1 has ,
     
    sumitt_2004, Jun 17, 2009 IP
  5. Nasukai

    Nasukai Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this (I don't know if it will make any difference but it never hurts to try):

    $query_cust="
    INSERT INTO `personal_details`
    VALUES (
    $cust_id,
    \'$cust_name\',
    \'$cust_photo\',
    \'$cust_post_area\',
    \'$cust_per_add\',
    \'$cust_force_batt\',
    \'$cust_irla\',
    \'$cust_ppo\',
    \'$cust_icard\',
    \'$cust_rank\',
    \'$dob\',
    \'$cust_bank_name\',
    \'$cust_bank_ac\',
    \'$cust_bank_branch\',
    \'$cust_pan\',
    \'$cust_r_phone\',
    \'$cust_o_phone\',
    \'$cust_mob\',
    \'$cust_email\',
    \'$cust_flat\',
    \'$cust_title\',
    \'$cust_rel\',
    \'$cust_rel_name\',
    \'$app_date\'
    );";
     
    Nasukai, Jun 17, 2009 IP
  6. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #6
    insert into table (field1,field2) values ('1','2') is the best way to go.
     
    dweebsonduty, Jun 27, 2009 IP