Try as I may, I keep getting an error querying database message, and just can't figure it out. I have shortened my code considerably to try narrowing it down, but still no luck. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Troop 97 Permission Form</title> <link rel="stylesheet" type="text/css" href="style.css" /> <style> .legal { font-size:11px; font-style:italic; } </style> </head> <body> <h2>Troop 97 Permission Slip</h2> <form method="post" action="permissiontest.php"> <label for="activity">Activity:</label> <input type="text" id="activity" name="activity" /><br /> <label for="when">Date(s):</label> <input type="text" id="when" name="when" /><br /> <label for="location">Location</label> <input type="text" id="location" name="location" /><br /> <label for="scoutname">Scout Name(s)</label> <input type="text" id="scoutname" name="scoutname" /><br /> <input type="submit" value="Submit Form" name="submit" /> </form> </body> </html> HTML: <?php $activity = $_POST['activity']; $when = $_POST['when']; $location = $_POST['location']; $scout_name = $_POST['scoutname']; $dbc = mysql_connect(localhost, '*******', '******'); mysql_select_db('troop97_permissions', $dbc) or die('Error connecting to MYSQL server.'); $query = "INSERT INTO permission_test (activity, when, location, scout_name, " "VALUES ('$activity', '$when', '$location', '$scout_name', " ; $result = mysql_query($query, $dbc) or die(mysql_error().'<br>SQL:'.$query); mysql_close($dbc); echo 'Thanks for submitting the permission form.<br>'; ?> PHP: Here is the error message: Parse error: syntax error, unexpected '"' in /home/troop97/public_html/permissiontest.php on line 24
Change the query to $query = "INSERT INTO permission_test (activity, when, location, scout_name) VALUES ('$activity', '$when', '$location', '$scout_name');"; PHP: If it will not help, write here the line 24 of the file permissiontest.php.
I agree..The values you are entering into the table permission_test should be within single quotes individually. Then the line must terminate with a semi colon and the total query should be within double quotes.
If you wanted ? a stripped down version, this would be the bare minimum. <?php $activity = $_POST['activity']; $when = $_POST['when']; $location = $_POST['location']; $scout_name = $_POST['scoutname']; mysql_connect(localhost, '*******', '******'); mysql_select_db('troop97_permissions'); $query = "INSERT INTO permission_test (activity, when, location, scout_name)VALUES('$activity', '$when', '$location', '$scout_name')"; $result = mysql_query($query); echo "Thanks for submitting the permission form.<br>"; ?> PHP:
Thanks - this last version of code worked. I haven't had time to look it over and compare to what was in the book I was using as a reference guide, but will try to review it carefully today and see where the differences were. Much of what I was using was code that was in the exercises in the book adapted to this new database. They worked as constructed in the book when I tested them, so now I need to learn from this experience and build up from there. Thanks to all for the input.
I think I spoke too soon. I thought it worked as it echoed that the submission was sent. Upon checking database nothing was received. I put back the or die portion and got back the error querying database message. I'm wondering if it has anything to do with the difference between scoutname and scout_name?
Sorry my bad, try this instead, but please do use your book, what I posted is just a stripped down version, I simple eliminated the error reporting and a few unnecessary variables to achieve what you wanted. $activity = $_POST['activity']; $when = $_POST['when']; $location = $_POST['location']; $scout_name = $_POST['scoutname']; mysql_connect(localhost, '*******', '******'); mysql_select_db('troop97_permissions'); $query = "INSERT INTO permission_test (activity, when, location, scout_name)VALUES('$activity', '$when', '$location', '$scout_name')"; mysql_query($query); echo "Thanks for submitting the permission form.<br>"; PHP: You could also do it this way: mysql_connect(localhost, '*******', '******'); mysql_select_db('troop97_permissions'); mysql_query("INSERT INTO permission_test (activity, when, location, scout_name)VALUES(' . $_POST['activity'] . ', ' . $_POST['when'] . ', ' . $_POST['location'] . ', ' . $_POST['scoutname'] . ')"); echo "Thanks for submitting the permission form.<br>"; PHP:
* Edit post * Delete post * Report this post * Reply with quote Re: Code Help Please Postby dmwesq » Thu Dec 09, 2010 11:46 am Here is my latest error message: A fatal MySQL error occured. Query: INSERT INTO permission_test (activity, when, location, scout_name)VALUES('Harriman Backpacking', 'Oct', 'Harriman Park', 'Ima Eagle') Error: (1064) 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 'when, location, scout_name)VALUES('Harriman Backpacking', 'Oct', 'Harriman Park'' at line 1 Server version: 5.0.91-community
I am also wondering if the problem lies with the collation settings for my database. My default code is set to utf8_unicode but even though I actually set this as well when setting up my database it keeps reverting to latin1_swedish_ci for the database. I'm sure this has got to be part of the problem, but I have no idea why it keeps changing.
Thanks for the responses - between the combination of cleaning up the code and avoiding protected words I now have it functioning and can move on to further developing the form.