Page 1 of 1

Code Help Please

Posted: Wed Dec 08, 2010 4:45 pm
by dmwesq
I'm working on creating a form, and keep getting an error querying database message. I assume the connection is fine and there is something wrong with the query itself, but I'm just not spotting the issue (or issues)..

Code: Select all

<?php
$activity = $_POST['activity'];
$date = $_POST['date'];
$location = $_POST['location'];
$scout_name = $_POST['scoutname'];
$attending = $_POST['attending'];
$parent = $_POST['parent'];
$primary_phone = $_POST['primaryphone'];
$alternate_phone = $_POST['alternatephone'];
$alternate_contact = $_POST['alternatecontact'];
$alternate_contact_phone = $_POST['alternatecontactphone'];
$physical = $_POST['physical'];
$other = $_POST['other'];
$medicine = $_POST['medicine'];
$other_2 = $_POST['other2'];
$transport = $_POST['transport'];
$transport_2 = $_POST['transport2'];
$other_3 = $_POST['other3'];
$legal = $_POST['legal'];

$dbc = mysql_connect(localhost, 'troop97_test', 'test1234');
mysql_select_db('troop97_permissions', $dbc)
or die('Error connecting to MYSQL server.');

$query = "INSERT INTO permission_form (activity, date, location, scout_name, " .
"attending, parent, primary_phone, alternate_phone, alternate_contact, alternate_contact_phone, " .
"physical, other, medicine, other_2, transport, transport_2, other_3, legal) " .
"VALUES ('$activity', '$date', '$location', '$scout_name', " .
"'$attending', '$parent', '$primary_phone', '$alternate_phone', '$alternate_contact', '$alternate_contact_phone', " .
"'$physical', '$other', '$medicine', '$other_2', '$transport', '$transport_2', '$other_3', 'legal') " ;

$result = mysql_query($query, $dbc)
or die('Error querying database.');

mysql_close($dbc);

echo 'Thanks for submitting the permission form.<br>';

?>
Here is the html code that goes with the php code:

[text]<!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="permission.php">
<label for="activity">Activity:</label>
<input type="text" id="activity" name="activity" /><br />
<label for="date">Date(s):</label>
<input type="text" id="date" name="date" /><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 />
<label for="attending">List any other people attending:</label>
<input type="text" id="attending" name="attending" /><br />
<label for="parent">Parent/Guardian Name</label>
<input type="text" id="parent" name="parent" /><br />
<label for="primaryphone">Primary Phone</label>
<input type="text" id="primaryphone" name="primaryphone" size="32" /><br />
<label for="alternatephone">Alternate Phone</label>
<input type="text" id="alternatephone" name="alternatephone" size="32" /><br />
<label for="alternatecontact">Alternate Contact</label>
<input type="text" id="alternatecontact" name="alternatecontact" size="32" /><br />
<label for="alternatecontactphone">Alternate Contact Phone</label>
<input type="text" id="alternatecontact" name="alternatecontact" size="32" /><br />
<label for="physical">Are there any changes to your child's (or your own) medical condition since his last BSA Medical Form was submitted?</label>
Yes <input id="physical" name="physical" type="radio" value="yes" />
No <input id="physical" name="physical" type="radio" value="no" /><br />

<label for="other">Describe changes</label>
<textarea id="other" name="other"></textarea><br />

<label for="medicine">Are there any changes to your child's (or your own) medications (including over the counter drugs) since his last BSA Medical Form was submitted?</label>
Yes <input id="medicine" name="medicine" type="radio" value="yes" />
No <input id="medicine" name="medicine" type="radio" value="no" /><br />

<label for="other2">Describe changes</label>
<textarea id="other2" name="other2"></textarea><br />
<p>It is each scout/family's responsibility to arange for transportation to any activity. While the troop will endeavor to accomodate scouts who do not have transportation, we cannot guarantee that room will be available.</p>
<label for="transport">Is your scout being transported to and/or from the activity by a parent or guardian?</label>
Yes <input id="transport" name="transport" type="radio" value="yes" />
No <input id="transport" name="transport" type="radio" value="no" /><br />

<label for="transport2">If no, have you arranged for transportation with another troop member?</label>
Yes <input id="transport2" name="transport2" type="radio" value="yes" />
No <input id="transport2" name="transport2" type="radio" value="no" /><br />

<label for="other3">If yes, provide name of family who will be transporting</label>
<input type="text" id="other3" name="other3" /><br />


<p class="legal"><input type="checkbox" name="legal[]" value="signed">As the parent or legal guardian of the above named scout(s) I hereby give my permission for my son(s) to participate in the above-mentioned activity. I give permission to the leaders of the above unit, to render first aid, should the need arise. In the event of an emergency, I also give permission to the adult leader in charge, to secure proper medical treatment, and for the medical provider(s) so selected to hospitalize, secure proper anesthesia, order injection, or secure other medical treatment, as needed. I further agree to hold the above named unit and its leaders blameless for any accidents that might occur during this outing except for clear acts of negligence or non-adherence to BSA policies and guidelines. </p>
<input type="submit" value="Submit Form" name="submit" />
</form>
</body>
</html>
[/text]

Re: Code Help Please

Posted: Wed Dec 08, 2010 5:08 pm
by social_experiment
A good way to find out where to look for the problem is to give the query the option to communicate the error.

Code: Select all

<?php
 $result = mysql_query($query, $dbc)
 //
 if (!$result) { echo mysql_error() }; 
?>
You can remove this for production but while you develop your code it saves time if you check for errors using php functions. 'Error querying database. tells you absolutely diddly about the problem.

I think your issue is with the construction of the query, try this code and see what happens

Code: Select all

<?php
$query = "INSERT INTO permission_form (activity, date, location, scout_name, 
attending, parent, primary_phone, alternate_phone, alternate_contact, alternate_contact_phone, 
physical, other, medicine, other_2, transport, transport_2, other_3, legal) 
VALUES ('$activity', '$date', '$primary_phone', '$alternate_phone', '$alternate_contact', 
'$alternate_contact_phone', '$attending', '$parent', '$primary_phone', '$alternate_phone', 
'$alternate_contact', '$alternate_contact_phone', '$physical', '$other', '$medicine', 
'$other_2', '$transport', '$transport_2', '$other_3', 'legal')";
?>
If you type and want to keep your lines to a certain length, break them off after a space, you don't have to concatenate the two lines using the concatenation operator (.) because to php it's still one line of code until a ; is reached. If the query isn't the problem you might check the amount of values you are trying to insert, if you have 3 fields in the table, you can only insert 3 values

Code: Select all

<?php
 // correct
 $query = mysql_query("INSERT INTO table (field1, field2) VALUES (val1, val2)");

 // will result in an error
 $query = mysql_query("INSERT INTO table (field1) VALUES (val1, val2)");
?>

Re: Code Help Please

Posted: Wed Dec 08, 2010 5:39 pm
by dmwesq
social_experiment wrote:A good way to find out where to look for the problem is to give the query the option to communicate the error.

Code: Select all

<?php
 $result = mysql_query($query, $dbc)
 //
 if (!$result) { echo mysql_error() }; 
?>
You can remove this for production but while you develop your code it saves time if you check for errors using php functions. 'Error querying database. tells you absolutely diddly about the problem.
Here is error message I get if I try using this code:

[text]Parse error: syntax error, unexpected T_IF in /home/troop97/public_html/permission.php on line 48[/text]

I think your issue is with the construction of the query, try this code and see what happens

Code: Select all

<?php
$query = "INSERT INTO permission_form (activity, date, location, scout_name, 
attending, parent, primary_phone, alternate_phone, alternate_contact, alternate_contact_phone, 
physical, other, medicine, other_2, transport, transport_2, other_3, legal) 
VALUES ('$activity', '$date', '$primary_phone', '$alternate_phone', '$alternate_contact', 
'$alternate_contact_phone', '$attending', '$parent', '$primary_phone', '$alternate_phone', 
'$alternate_contact', '$alternate_contact_phone', '$physical', '$other', '$medicine', 
'$other_2', '$transport', '$transport_2', '$other_3', 'legal')";
?>
If you type and want to keep your lines to a certain length, break them off after a space, you don't have to concatenate the two lines using the concatenation operator (.) because to php it's still one line of code until a ; is reached.

This code made no difference. I used the concatenation operator because that was how the samples in the php book I was modeling off of had the code when it spanned several lines. I used that format in the examples I was working with and they worked just fine.
If the query isn't the problem you might check the amount of values you are trying to insert, if you have 3 fields in the table, you can only insert 3 values

Code: Select all

<?php
 // correct
 $query = mysql_query("INSERT INTO table (field1, field2) VALUES (val1, val2)");

 // will result in an error
 $query = mysql_query("INSERT INTO table (field1) VALUES (val1, val2)");
?>
The amount of fields in the table matches the amount in the php code.

Re: Code Help Please

Posted: Thu Dec 09, 2010 1:08 am
by social_experiment
dmwesq wrote:Parse error: syntax error, unexpected T_IF in /home/troop97/public_html/permission.php on line 48
This is a php syntax error, not a database error. Please paste the code for 'permission.php'.
dmwesq wrote:This code made no difference. I used the concatenation operator because that was how the samples in the php book I was modeling off of had the code when it spanned several lines. I used that format in the examples I was working with and they worked just fine.
My bad.
dmwesq wrote:The amount of fields in the table matches the amount in the php code.
Im not disputing this :) The problem is when you have 18 fields (as you do) and you try to insert 20 values into them. What does the database do with the additional 2?

Re: Code Help Please

Posted: Thu Dec 09, 2010 1:52 am
by dmwesq
I'm not sure I follow you. I only count 18 values, unless you are referring to the 2 radio button fields. From the practice tables I used, the radio buttons, although alloting a choice between items, only counted as one field in the table. Other than that I count 18 items in my html file, and 18 in my php code. Not sure where you are getting 20 from.

In any event, I also tried shortening everything by creating a new table with only 4 fields, and using the code:
[text]<!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>[/text]

Code: Select all

<?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>';

?>
and get the following error:
Parse error: syntax error, unexpected '"' in /home/troop97/public_html/permissiontest.php on line 24

If I can get past this point I will try to build back to all the items I wanted in one by one and see what happens.

Re: Code Help Please

Posted: Thu Dec 09, 2010 2:13 am
by social_experiment
dmwesq wrote:Not sure where you are getting 20 from.
Yeah sorry, that is a mistake on my part. I must have typed in addtional variables when copying your code 8O

Code: Select all

<?php
// your query missing 2 parenthesis and with 2 quotation marks.
$query = "INSERT INTO permission_test (activity, when, location, scout_name, "
"VALUES ('$activity', '$when', '$location', '$scout_name', " ;

// the query with all parenthesis and no quotation marks.
$query = "INSERT INTO permission_test (activity, when, location, scout_name) 
VALUES ('$activity', '$when', '$location', '$scout_name')";
?>

Re: Code Help Please

Posted: Thu Dec 09, 2010 10:46 am
by dmwesq
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

Re: Code Help Please

Posted: Thu Dec 09, 2010 11:27 am
by dmwesq
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.

Re: Code Help Please

Posted: Thu Dec 09, 2010 4:27 pm
by social_experiment
As per the query error, check for reserved words in your query. 'When' is a reserved word according to the list at this url http://developer.mimer.com/validator/sq ... -words.tml, maybe it's got something to do with that.

Re: Code Help Please

Posted: Fri Dec 10, 2010 1:25 am
by dmwesq
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.