My PHP form data is not going into my SQL database. Is my syntax correct? My code is:
<?php
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$pass = $_POST['password'];
$repass = $_POST['retypepass'];
$street = $_POST['street'];
$phone = $_POST['phone'];
$apt = $_POST['apt'];
$city = $_POST['city'];
$state = $_POST['state'];
$birthday_month = $_POST['birthday_month'];
$birthday_day = $_POST['birthday_day'];
$birthday_years = $_POST['birthday_years'];
$gender = $_POST['gender'];
$zip = $_POST['zip'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$reanswer = $_POST['reanswer'];
$registration = $_POST['registration'];
$connect = mysql_connect('my host, 'username', 'password') or die("Unable to Connect to Database<br>".mysql_error());
mysql_select_db('my host') or die("Unable to Select Database<br>".mysql_error());
mysql_query ("INSERT INTO 'user' VALUES ('$name','$lastname','$email','$pass','$repass','$street','$phone','$apt','$city','$birthday_month','$birthday_day','$birthday_year','$gender','$zip','$question','$answer','$reanswer','$registration')");
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database. Thank you for your registration.;
?>
****** Please note, I replaced my host, username and password with generic info.
SQL database will not take PHP Form data
Moderator: General Moderators
-
visionmaster
- Forum Contributor
- Posts: 139
- Joined: Wed Jul 14, 2004 4:06 am
Re: SQL database will not take PHP Form data
Hello,
The first thing you should output for debugging reasons is the value of your $_POST, to make sure that data is being passed on. Do this using by inserting var_dump( $_POST ); into your code.
If your $_POST holds data, then continue with analyzing your MySQL-query.
Change your code like this:
The echo outputs your SQL-statement, which you then can copy and insert into phpMyAdmin (or whatever you are using). Then you can see if your MySQL statement is correct. Please consult following webpage for the INSERT-syntax, http://dev.mysql.com/doc/refman/5.0/en/insert.html
Regards
The first thing you should output for debugging reasons is the value of your $_POST, to make sure that data is being passed on. Do this using by inserting var_dump( $_POST ); into your code.
If your $_POST holds data, then continue with analyzing your MySQL-query.
Change your code like this:
Code: Select all
[...]
$strSQL = "INSERT INTO 'user' VALUES ('$name','$lastname','$email','$pass','$repass','$street','$phone','$apt','$city','$birthday_month','$birthday_day','$birthday_year','$gender','$zip','$question','$answer','$reanswer','$registration')"
echo $strSQL;
mysql_query($strSQL);
[...]
Regards
Re: SQL database will not take PHP Form data
Code: Select all
"INSERT INTO [b]'user'[/b] VALUES...In addition, you have zero data validation and don't escape the incoming data at all.
Re: SQL database will not take PHP Form data
Thank you both
I tried both suggestions. The results of the output is:
INSERT INTO 'user' VALUES ('Sallyann','Smith','smith@gmail.com','smith','123','123','55 test drive','1','Winterville','Kansas','10017','5555555555','September','15','1978','Female','What is your favorite color?','Blue','Blue','ps663b8h') Your Information has been successfully added to the database.
Thank you for your registration.
The I also tried to take the quotes out of 'user' but that didn't do anything. I still don't have any info in my sql table.
** As far as data validation I used javascript validation in the form.
Are there any more suggestions?
I tried both suggestions. The results of the output is:
INSERT INTO 'user' VALUES ('Sallyann','Smith','smith@gmail.com','smith','123','123','55 test drive','1','Winterville','Kansas','10017','5555555555','September','15','1978','Female','What is your favorite color?','Blue','Blue','ps663b8h') Your Information has been successfully added to the database.
Thank you for your registration.
The I also tried to take the quotes out of 'user' but that didn't do anything. I still don't have any info in my sql table.
** As far as data validation I used javascript validation in the form.
Are there any more suggestions?
Re: SQL database will not take PHP Form data
I copied my SQL output into myphpadmin and the information went in right away. There is something wrong with the way it communicating from php. Does someone else have a working sample or good tutorial of a SQL & PHP form? Also does the order of my query matter?
I updated the code. It now is:
$connect = mysql_connect('ip', 'database name', 'password') or die("Unable to Connect to Database<br>".mysql_error());
mysql_select_db('database) or die("Unable to Select Database<br>".mysql_error());
$query="INSERT INTO user (username, email, password, registration_date, name,lastname, email, pass, repass, street, phone, apt, city, birthday_month, birthday_day, birthday_year, gender, zip, question, answer, reanswer, registration) VALUES('$name','$lastname','$email','$pass','$repass','$street','$phone','$apt','$city','$birthday_month','$birthday_day','$birthday_year','$gender','$zip','$question','$answer','$reanswer','$registration')"or die( mysql_error() );
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database.";
I updated the code. It now is:
$connect = mysql_connect('ip', 'database name', 'password') or die("Unable to Connect to Database<br>".mysql_error());
mysql_select_db('database) or die("Unable to Select Database<br>".mysql_error());
$query="INSERT INTO user (username, email, password, registration_date, name,lastname, email, pass, repass, street, phone, apt, city, birthday_month, birthday_day, birthday_year, gender, zip, question, answer, reanswer, registration) VALUES('$name','$lastname','$email','$pass','$repass','$street','$phone','$apt','$city','$birthday_month','$birthday_day','$birthday_year','$gender','$zip','$question','$answer','$reanswer','$registration')"or die( mysql_error() );
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database.";