CrownVictoriaCop Posted September 27, 2009 Share Posted September 27, 2009 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."')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/ Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925911 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925914 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 Oh, 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."')"); Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925916 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 No PHP or MySQL Errors, but the test information isn't being written into the database, according to PHPMyAdmin. Oh, 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."')"); Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925917 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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(); Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925923 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 Table 'safetyfi_students.Students' doesn't exist I was told I need to run an SQL query on my database before this script could work. Could you tell me what query I should run, because I don't know that much SQL Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925927 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 You'll need to run a query to create the table in the database. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925933 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 So, what would that be? Can you please give me the entire query to run on my database? I would appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925934 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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, ... ); Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925937 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 I used a tutorial on teamtutorials.com and edited it to fit my needs. No SQL on the page. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925938 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 how was you going to enter the information then, with no sql configuration in the database. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925940 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 I didn't know. I'm a newbie at PHP and MySQL. how was you going to enter the information then, with no sql configuration in the database. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925943 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 your have to learn how to create a database first. usually in phpmyadmin for ease of use? you got that phpmyadmin? Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925945 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925948 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 Zip INT NOT NULL, <<<< use varchar can be number or letter? Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925950 Share on other sites More sharing options...
PFMaBiSmAd Posted September 27, 2009 Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925956 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925960 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925963 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 Wow! It works! Thank you guys so much! I really do appreciate how kind this forum is to n00bs of php. Quote Link to comment https://forums.phpfreaks.com/topic/175710-solved-php-not-writing-to-mysql-database/#findComment-925966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.