SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist jimday1982's Avatar
    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Slight problem with insert query

    I have a form that should be inserting data into a database, but apparently something is up with my insert query...all the fieldnames are correct, as are the variables...do you see where I'm going wrong?

    PHP Code:
    //Insert member data into members table
     
    $first_name $_POST["first_name"];
     
    $last_name $_POST["last_name"];
     
    $address1 $_POST["address1"];
     
    $address2 $_POST["address2"]; 
     
    $city $_POST["city"];
     
    $state $_POST["state"];
     
    $zip $_POST["zip"];
     
    $home_phone $_POST["home_phone"];
     
    $work_phone $_POST["work_phone"];
     
    $mobile_phone $_POST["mobile_phone"];
     
    $email $_POST["email"];
     
    $dob_month $_POST["dob_month"];
     
    $dob_day $_POST["dob_day"];
     
    $dob_year $_POST["dob_year"];
     
    $gender $_POST["gender"];
     
    $military $_POST["military"];
     
     
    $link mysql_connect('blah''blah''blah');
     if (! 
    $link)
     die(
    "Couldn't connect to MySQL");
     
    mysql_select_db('tacticalshooting' $link)
     or die(
    "Select DB Error: ".mysql_error());
     
    //insert data
     
    $insert mysql_query ("INSERT INTO members (first_name, last_name, address1, address2, city, state, zip, home_phone, work_phone, mobile_phone, email, dob_month, dob_day, dob_year, gender, military) VALUES ($first_name$last_name$address1$address2$city$state$zip$home_phone$work_phone$mobile_phone$email$dob_month$dob_day$dob_year$gender$military)");
     if (! 
    $insert)
     die(
    mysql_error());
     
    mysql_close($link); 
    The error I'm getting is: 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 'by Jim, TEST, TEST, TEST, 1, 1, 2005, Male, No)' at line 1

    Thanks for your help!
    Jimmy Day
    Senior Systems Analyst
    Piedmont Healthcare Corporation

  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think not quoting the text values will cause this error.

  3. #3
    SitePoint Evangelist jimday1982's Avatar
    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coo_t2
    I think not quoting the text values will cause this error.
    Ah, you are correct. Thanks!
    Jimmy Day
    Senior Systems Analyst
    Piedmont Healthcare Corporation

  4. #4
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't $_POST["first_name"]; be $_POST[first_name];?
    I am just learning but that's what I usually use

  5. #5
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually use single quotes for my post variables.

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Mav3n
    Shouldn't $_POST["first_name"]; be $_POST[first_name];?
    I am just learning but that's what I usually use
    No. first_name is a string-based key, so it should be quoted. If you use it inside of another string you don't need to quote it.
    Like this:
    $sql = "SELECT * FROM tableName WHERE name='$_POST[name]'";

    --ed


Bookmarks

Posting Permissions

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