something is wrong with this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

something is wrong with this code

Post 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
Last edited by jmilane on Wed Sep 20, 2006 1:41 pm, edited 2 times in total.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Try wrapping the code with

Code: Select all

'$value'
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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' ...
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Post 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'];
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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.
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Post 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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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!
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

jmilane I say you better remove the username/passwords/site-address data from your post...
Post Reply