...

View Full Version : SQL Query not running :S



rafiki
09-03-2006, 10:39 PM
this is basically my first mysql experience

heres the query

$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>");
}

NancyJ
09-03-2006, 10:53 PM
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

rafiki
09-03-2006, 11:00 PM
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

NancyJ
09-04-2006, 12:19 PM
Did you try removing the {} from the field names as I suggested?

rafiki
09-04-2006, 03:45 PM
yes error message returned it
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

googleit
09-04-2006, 03:54 PM
get rid of all the brackets on the first line

rafiki
09-04-2006, 07:01 PM
$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

googleit
09-04-2006, 07:16 PM
hmm read this page (http://www.tizag.com/mysqlTutorial/mysqlinsert.php) it may give some help

NancyJ
09-04-2006, 09:05 PM
you havent put quotes around your values

GJay
09-04-2006, 09:36 PM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum