Page 1 of 1

something is wrong with this code

Posted: Wed Sep 20, 2006 1:25 pm
by jmilane
I know this is really basic, but it took me hours to put together.

See anything wrong?

Thanks in advance.

Code: Select all

<?PHP

$thismonth = $_POST[thismonth];
$thisday = $_POST[thisday];
$thisyear = $_POST[thisyear];

$employmentprog = $_POST[employment_chkbox];
$businessprog = $_POST[business_chkbox];
$certprog = $_POST[certification_chkbox];
$youthprog = $_POST[youth_chkbox];
$parentprog = $_POST[parent_chkbox];
$policyprog = $_POST[advocacy_chkbox];
$firstname = $_POST[employment_chkbox];
$middlename = $_POST[middlename];
$month = $_POST['birth_date'][M];
$day = $_POST['birth_date'][d]; // I *think* this is the right way to grab an element of an array???
$year = $_POST['birth_date'][Y];
$birthdate = $year.'-'.$month.'-'.$day;
$street = $_POST[street];
$city = $_POST[city];
$state = $_POST[state];
$postalcode = $_POST[postalcode];
$homephone = $_POST[homephone];
$otherphone = $_POST[otherphone];
$referral = $_POST[referral];
$contact = $_POST[contact];
$race = $_POST[race];
$otherrace = $_POST[otherrace];
$gender = $_POST[gender];
$registrationday =  $thisyear.'-'.$thismonth.'-'.$thisday;




$DBhost = "www.usdm.org";
$DBuser = "jsdde";
$DBpass = "gsy";
$DBName = "ulem";
$table = "registration";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName"); 

$sqlquery = "INSERT INTO registration (employmentprog, businessprog, certprog, youthprog, parentprog, policyprog, parentprog, policyprog, firstname, middlename, lastname, birthdate, street, city, state, postalcode, homephone, otherphone, referral, contact, race, otherrace, gender, regdate)
VALUES
($employmentprog, $businessprog, $certprog, $youthprog, $parentprog, $policyprog, $parentprog, $policyprog, $firstname, $middlename,$lastname, $birthdate, $street, $city, $state, $postalcode, $homephone, $otherphone, r$eferral, $contact, $race, $otherrace, $gender, $registrationday)";

$result = mysql_query($sqlquery);

?>
Nothing is getting inserted.

Just a blank page comes up.

Thanks for your attentions...

J

Posted: Wed Sep 20, 2006 1:30 pm
by impulse()
Try wrapping the code with

Code: Select all

'$value'

Posted: Wed Sep 20, 2006 1:31 pm
by ok
1. I think that you should add inverted commas to "$_POST[thisyear]".
2. If you insert a string to the DB, you should delimit the string like that:

Code: Select all

... 'My String1', 'My String2' ...

Posted: Wed Sep 20, 2006 1:32 pm
by SpecialK
You shouldn't be using constants to pull out of a post

Code: Select all

$thismonth = $_POST[thismonth];
It should be surrounded with quotes

Code: Select all

$thismonth = $_POST['thismonth'];
The birth date should be done the same way, provided this is how the form is designed

Code: Select all

$month = $_POST['birth_date']['M'];

Posted: Wed Sep 20, 2006 1:49 pm
by jmilane
Here is the new code:

Code: Select all

<?PHP

$thismonth = $_POST['thismonth'];
$thisday = $_POST['thisday'];
$thisyear = $_POST['thisyear'];

$employmentprog = $_POST['employment_chkbox'];
$businessprog = $_POST['business_chkbox'];
$certprog = $_POST['certification_chkbox'];
$youthprog = $_POST['youth_chkbox'];
$parentprog = $_POST['parent_chkbox'];
$policyprog = $_POST['advocacy_chkbox'];
$firstname = $_POST['employment_chkbox'];
$middlename = $_POST['middlename'];
$month = $_POST['birth_date']['M'];
$day = $_POST['birth_date']['d'];
$year = $_POST['birth_date']['Y'];
$birthdate = $year.'-'.$month.'-'.$day;
$street = $_POST['street'];
$city = $_POST['city'];
$state = $_POST['state'];
$postalcode = $_POST['postalcode'];
$homephone = $_POST['homephone'];
$otherphone = $_POST['otherphone'];
$referral = $_POST['referral'];
$contact = $_POST['contact'];
$race = $_POST['race'];
$otherrace = $_POST['otherrace'];
$gender = $_POST['gender'];
$registrationday =  $thisyear.'-'.$thismonth.'-'.$thisday;




$DBhost = "www.usdfm.org";
$DBuser = "jsdfne";
$DBpass = "gs00gly";
$DBName = "uldm";
$table = "registration";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName"); 

$sqlquery = "INSERT INTO registration (employmentprog, businessprog, certprog, youthprog, parentprog, policyprog, parentprog, policyprog, firstname, middlename, lastname, birthdate, street, city, state, postalcode, homephone, otherphone, referral, contact, race, otherrace, gender, regdate)
VALUES
($employmentprog, $businessprog, $certprog, $youthprog, $parentprog, $policyprog, $parentprog, $policyprog, $firstname, $middlename,$lastname, $birthdate, $street, $city, $state, $postalcode, $homephone, $otherphone, $referral, $contact, $race, $otherrace, $gender, $registrationday)";

$result = mysql_query($sqlquery);

?>
No dice.

How can I tell what is going on? It looks good to me. For sure.

Thank you.

Posted: Wed Sep 20, 2006 1:56 pm
by SpecialK
With SQL I usually like to echo data before and after the insert to error check. For all other errors I use the logfiles

What impulse() was saying has to do with SQL Inserts. When expecting a varchar you need to wrap your strings in quotes.

Code: Select all

$sqlquery = "INSERT INTO registration (employmentprog, businessprog, certprog, youthprog, parentprog, policyprog, parentprog, policyprog, firstname, middlename, lastname, birthdate, street, city, state, postalcode, homephone, otherphone, referral, contact, race, otherrace, gender, regdate)
VALUES
('$employmentprog','$businessprog', '$certprog', '$youthprog', '$parentprog', '$policyprog', '$parentprog', '$policyprog', '$firstname', '$middlename','$lastname', '$birthdate', '$street', '$city', '$state', '$postalcode', '$homephone', '$otherphone', '$referral', '$contact', '$race', '$otherrace', '$gender', '$registrationday')";
You don't need to do this for numeric values, although I am unsure of date values because I haven't used mysql in ages.

Posted: Wed Sep 20, 2006 1:57 pm
by andym01480

Code: Select all

$sqlquery = "INSERT INTO registration (employmentprog, businessprog, certprog, youthprog, parentprog, policyprog, parentprog, policyprog, firstname, middlename, lastname, birthdate, street, city, state, postalcode, homephone, otherphone, referral, contact, race, otherrace, gender, regdate)
VALUES
('$employmentprog', '$businessprog', '$certprog', '$youthprog', '$parentprog', '$policyprog', $parentprog, $policyprog, $firstname, $middlename,$lastname, $birthdate, $street, $city, $state, $postalcode, $homephone, $otherphone, $referral, $contact, $race, $otherrace, $gender, $registrationday)";

$result = mysql_query($sqlquery) or die (mysql_error());
Put the rest of teh VALUES variables in single quotes and see what the mysql_error() throws up!

Posted: Wed Sep 20, 2006 2:32 pm
by jmilane
andym01480 wrote:

Code: Select all

$sqlquery = "INSERT INTO registration (employmentprog, businessprog, certprog, youthprog, parentprog, policyprog, parentprog, policyprog, firstname, middlename, lastname, birthdate, street, city, state, postalcode, homephone, otherphone, referral, contact, race, otherrace, gender, regdate)
VALUES
('$employmentprog', '$businessprog', '$certprog', '$youthprog', '$parentprog', '$policyprog', $parentprog, $policyprog, $firstname, $middlename,$lastname, $birthdate, $street, $city, $state, $postalcode, $homephone, $otherphone, $referral, $contact, $race, $otherrace, $gender, $registrationday)";

$result = mysql_query($sqlquery) or die (mysql_error());
Put the rest of teh VALUES variables in single quotes and see what the mysql_error() throws up!
Column 'policyprog' specified twice

You da man!!!

Then:

Column count doesn't match value count at row 1

I had four extra fields!

Then:

Field 'id' doesn't have a default value

I made it auto_increment (I know I did that already, but must have undone it!)

AND IT WORKS!!!!!!!!!!!!!!!!!!!! YAHHOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!

Now I have to figure out how to

1. Make sure required fields get filled in.

2. Send them to a specific page after they fill it out.

You guys rock... thanks so much.

YAH!

Posted: Wed Sep 20, 2006 4:04 pm
by Mordred

Code: Select all

$sqlquery = "INSERT INTO registration SET
employmentprog = '$employmentprog',
.....
More readable and absolutely no way to get the number or the order of the variables wrong (note that a sintactically correct query could be a semantic nonsense if you have the order wrong, so this is a potential spot for nasty bugs.)

Posted: Wed Sep 20, 2006 9:30 pm
by n00b Saibot
jmilane I say you better remove the username/passwords/site-address data from your post...