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 10 of 10
  1. #1
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts

    SQL Query not running :S

    this is basically my first mysql experience

    heres the query
    Code:
    $sql = "INSERT INTO {students} ({firstname}, {surname}, {dob}, {school_year}, {bedtime}, {wakeup}, {homework}, 
    {tv}, {computer}, {friends}, {family}) VALUES ('{$_POST['firsname']}','{$_POST['surname']}','{$_POST['dob']}','{$_POST['school_year']}',
    '{$_POST['bedtime']}','{$_POST['wakeup']}',
    '{$_POST['homework']}','{$_POST['tv']}','{$_POST['computer']}','{$_POST['friends']}','{$_POST['family']}'); ;";
    $result = mysql_query($sql);
    
    // As usual, check to make sure that it worked:
    if (!$result)
    {
      print("<h2>Failed to run the query! Error is:" . mysql_error(). "</h2>");
    }
    else
    {
      print("<p>Query ran successfully!</p>");
    }

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    For future reference it helps to say what the problem is. Look at your code I would say you should have got a mysql_error message if the query failed. Did you?
    The {} are only needed around the php array values. I dont know if they get parsed out when they're just around text. That might be the problem. Echo out the query and let us know if you are getting any error messages

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Code:
    Failed to run the query! Error 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 '{students} ({firstname}, {surname}, {dob}, {school_year}, {bedt

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Did you try removing the {} from the field names as I suggested?

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    yes error message returned it
    Code:
    Step 3: Run an SQL query!
    Failed to run the query! Error 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 '{students} (firstname, surname, dob, school_year, bedtime, wake

  • #6
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    get rid of all the brackets on the first line

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Code:
    $sql = "INSERT INTO students  firstname, surname, dob, school_year, bedtime, wakeup, homework, 
    tv, computer, friends, family VALUES ({$_POST['firstname']}, {$_POST['surname']}, {$_POST['DOB']}, 
    {$_POST['school_year']},  {$_POST['siblings']},  {$_POST['bedtime']}, 
     {$_POST['homework']}, {$_POST['tv']},  {$_POST['computer']}, 
     {$_POST['family']},  {$_POST['friends']}'); ;";
    $result = mysql_query($sql);
    still returns errors

  • #8
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm read this page it may give some help

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    you havent put quotes around your values

  • #10
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    as said, inserted strings need quotes, also the fieldnames on the first line should have ()s around them (not around each one, but around the entire comma-separated bit)


  •  

    Posting Permissions

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