Jump to content

[SOLVED] PHP Not Writing To MySQL Database


CrownVictoriaCop

Recommended Posts

Hello, I'm using the following code to write information from a form to a MySQL Database. I can run the script with no PHP errors, however, the data is not being written into the database. I heard there is an SQL query I have to run. Which query would that be? Here is my code.

<?php

$PFName = $_POST['ParentFirstName'];

$PLName = $_POST['ParentLastName'];

$SFName = $_POST['StudentName'];

$Class = $_POST['Class'];

$Address = $_POST['Address'];

$City = $_POST['City'];

$State = $_POST['State'];

$Zip = $_POST['Zip'];

$Phone = $_POST['Phone'];

$BMonth = $_POST['Month'];

$BDay = $_POST['Day'];

$BYear = $_POST['Year'];

$School = $_POST['School'];

$Payment = $_POST['PaymentMethod'];

 

mysql_connect ("localhost", "safetyfi_secure", "HIDDEN") or die ("Your enrollment was not successful. Please contact the office, mentioning the error ' . mysql_error()");

mysql_select_db ("safetyfi_students");

 

mysql_query("INSERT INTO Students (ID, ParentFirstName, ParentLastName, StudentName, Class, Address, City, State, Zip, Phone, BirthMonth, BirthDay, BirthYear, School, PaymentMethod) VALUES 'NULL', '".$PFName."', '".$PLName."', '".$SFName."', '".$Class."', '".$Address."', '".$City."', '".$State."', '".$Zip."', '".$Phone."', '".$BMonth."', '".$BYear."', '".$School."', '".$PaymentMethod."')");

?>

Link to comment
Share on other sites

I don't see any problem with that code (besides that fact that you should be securing all data being inserted into the database with mysql_real_escape_string()). Try changing the query to this and tell us if it outputs any error:

 

mysql_query("INSERT INTO Students (ID, ParentFirstName, ParentLastName, StudentName, Class, Address, City, State, Zip, Phone, BirthMonth, BirthDay, BirthYear, School, PaymentMethod) VALUES 'NULL', '".$PFName."', '".$PLName."', '".$SFName."', '".$Class."', '".$Address."', '".$City."', '".$State."', '".$Zip."', '".$Phone."', '".$BMonth."', '".$BYear."', '".$School."', '".$PaymentMethod."')") or die(mysql_error());

Link to comment
Share on other sites

I get the error

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 ''NULL', 'Test', 'Tester', 'Testisico', 'Class 1', '123 Main Street', 'Milwaukee'' at line 1

I don't see any problem with that code (besides that fact that you should be securing all data being inserted into the database with mysql_real_escape_string()). Try changing the query to this and tell us if it outputs any error:

 

mysql_query("INSERT INTO Students (ID, ParentFirstName, ParentLastName, StudentName, Class, Address, City, State, Zip, Phone, BirthMonth, BirthDay, BirthYear, School, PaymentMethod) VALUES 'NULL', '".$PFName."', '".$PLName."', '".$SFName."', '".$Class."', '".$Address."', '".$City."', '".$State."', '".$Zip."', '".$Phone."', '".$BMonth."', '".$BYear."', '".$School."', '".$PaymentMethod."')") or die(mysql_error());

Link to comment
Share on other sites

Oh, :P You're missing a ( right after VALUES. Try this:

 

mysql_query("INSERT INTO Students (ID, ParentFirstName, ParentLastName, StudentName, Class, Address, City, State, Zip, Phone, BirthMonth, BirthDay, BirthYear, School, PaymentMethod) VALUES ('NULL', '".$PFName."', '".$PLName."', '".$SFName."', '".$Class."', '".$Address."', '".$City."', '".$State."', '".$Zip."', '".$Phone."', '".$BMonth."', '".$BYear."', '".$School."', '".$PaymentMethod."')");

Link to comment
Share on other sites

No PHP or MySQL Errors, but the test information isn't being written into the database, according to PHPMyAdmin.

Oh, :P You're missing a ( right after VALUES. Try this:

 

mysql_query("INSERT INTO Students (ID, ParentFirstName, ParentLastName, StudentName, Class, Address, City, State, Zip, Phone, BirthMonth, BirthDay, BirthYear, School, PaymentMethod) VALUES ('NULL', '".$PFName."', '".$PLName."', '".$SFName."', '".$Class."', '".$Address."', '".$City."', '".$State."', '".$Zip."', '".$Phone."', '".$BMonth."', '".$BYear."', '".$School."', '".$PaymentMethod."')");

Link to comment
Share on other sites

Is ID an auto_increment field? If so it's not even necessary to include it in the query. Try:

 

$result = mysql_query("INSERT INTO Students (ParentFirstName, ParentLastName, StudentName, Class, Address, City, State, Zip, Phone, BirthMonth, BirthDay, BirthYear, School, PaymentMethod) VALUES ('".$PFName."', '".$PLName."', '".$SFName."', '".$Class."', '".$Address."', '".$City."', '".$State."', '".$Zip."', '".$Phone."', '".$BMonth."', '".$BYear."', '".$School."', '".$PaymentMethod."')");
echo ($result) ? 'Success!' : mysql_error();

Link to comment
Share on other sites

Where'd you get the rest of this code from? If it's from a third-party source they should've supplied you with the SQL query to create the table. It would probably look something like..

 

CREATE TABLE IF NOT EXISTS `Students`
(
     ParentFirstName VARCHAR(32) NOT NULL,
     ...
);

Link to comment
Share on other sites

If you have a database created you can try this SQL query on it to create the table.

 

CREATE TABLE IF NOT EXISTS `Students`
(
ID INT NOT NULL AUTO_INCREMENT,
ParentFirstName VARCHAR(32) NOT NULL,
ParentLastName VARCHAR(32) NOT NULL,
StudentName VARCHAR(32) NOT NULL,
Class VARCHAR(32) NOT NULL,
Address VARCHAR(64) NOT NULL,
City VARCHAR(32) NOT NULL,
State VARCHAR(32) NOT NULL,
Zip INT NOT NULL,
Phone VARCHAR(32) NOT NULL,
BirthMonth VARCHAR(32) NOT NULL,
BirthDay VARCHAR(16) NOT NULL,
BirthYear VARCHAR(16) NOT NULL,
School VARCHAR(32) NOT NULL,
PaymentMethod VARCHAR(32) NOT NULL,

PRIMARY KEY(`ID`)
);

 

It's not optimized, it could (and should) be done differently but I'm not sure how of your form is setup and the format of the data.

 

Note: This will throw an error if you leave any of the fields blank; so you should also put in some form validation (currently the SQL query is assuming that you want every field required).

Link to comment
Share on other sites

CrownVictoriaCop, the site you found seems to be a random collection of information and at least the part I looked at concerning the mysql information was not one contiguous entity that started from the beginning and built upon the previous steps.

 

You would do much better starting with the mysql tutorial in the following link as it at least starts at the beginning and builds onto each step by using what has already come before - http://w3schools.com/php/php_mysql_intro.asp The w3schools.com site is also one of the better sites for basic web related tutorials.

Link to comment
Share on other sites

Executed Alex's code successfully on database. Tried the script again and got the message

Column count doesn't match value count at row 1

You're getting that error because the number of rows to insert data into don't match number of values you provided.

 

Edit: More specifically you don't have a value listed for BirthDay.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.