insert into db

Discussion in 'PHP' started by Bannanaman, Apr 5, 2009.

  1. #1
    the following error:

    Error: 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 2

    using the following code to do it:

    <?php
    $con = mysql_connect("localhost","deedpoll_adult","adult");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("deedpoll_adultpurchaseinfo", $con);
    
    $sql="INSERT INTO adultpurchaseinfo (adult_current_full_name, adult_desired, DOB_day, DOB_month, DOB_year, whereborn_radiobutton, country_individual_born_in, country_individual_father_born_in,country_individual_mother_born_in,cameuk_day,cameuk_month,cameuk_year,immigration_status,date_radiobutton,dated_day,dated_month,dated_year,house_number,street,line_2,town,county,postcode,scode)
    VALUES('$_POST[adult_current_full_name]','$_POST[adult_desired]','$_POST[DOB_day]','$_POST[DOB_month]','$_POST[DOB_year]','$_POST[whereborn_radiobutton]','$_POST[country_individual_born_in]''$_POST[country_individual_father_born_in]','$_POST[country_individual_mother_born_in]','$_POST[cameuk_day]','$_POST[cameuk_month]','$_POST[cameuk_year]','$_POST[immigration_status]','$_POST[date_radiobutton]','$_POST[dated_day]','$_POST[dated_month]','$_POST[dated_year]','$_POST[house_number]','$_POST[street]','$_POST[line_2]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[scode]',)";  
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    
    mysql_close($con)
    ?> 
    PHP:
    I just cant see the error - anyone else?
     
    Bannanaman, Apr 5, 2009 IP
  2. kakek

    kakek Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this

    $sql="INSERT INTO adultpurchaseinfo (adult_current_full_name, adult_desired, DOB_day, DOB_month, DOB_year,
    whereborn_radiobutton, country_individual_born_in, country_individual_father_born_in,country_individual_mother_born_in,
    cameuk_day,cameuk_month,cameuk_year,immigration_status,date_radiobutton,dated_day,dated_month,
    dated_year,house_number,street,line_2,town,county,postcode,scode)
    VALUES('$_POST[adult_current_full_name]','$_POST[adult_desired]','$_POST[DOB_day]','$_POST[DOB_month]',
    '$_POST[DOB_year]','$_POST[whereborn_radiobutton]','$_POST[country_individual_born_in]',
    '$_POST[country_individual_father_born_in]','$_POST[country_individual_mother_born_in]','$_POST[cameuk_day]',
    '$_POST[cameuk_month]','$_POST[cameuk_year]','$_POST[immigration_status]','$_POST[date_radiobutton]',
    '$_POST[dated_day]','$_POST[dated_month]','$_POST[dated_year]','$_POST[house_number]','$_POST[street]',
    '$_POST[line_2]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[scode]')";
     
    kakek, Apr 5, 2009 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    As corrected by Kakek, you didn't have a comma between $_POST[country_individual_born_in] and $_POST[country_individual_father_born_in].

    You may also have other errors depending on what the values of the variables can be. Remember that - and I'm not sure how accurate this is - strings have to be enclosed in quotation marks, NULL values must not be enclosed in quotation marks, and numerals do not have to be enclosed in quotation marks.
     
    Masterful, Apr 5, 2009 IP
  4. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you're going straight from an unescaped string, you really should use the '".$_POST['var']."' syntax to prevent the string from being closed by a hanging single quote in one variable. Numbers technically don't need to have single quotes around them.
     
    Altari, Apr 5, 2009 IP
  5. Bannanaman

    Bannanaman Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks all, resolved
     
    Bannanaman, Apr 5, 2009 IP