Page 1 of 1

SQL database will not take PHP Form data

Posted: Fri Apr 11, 2008 10:59 pm
by latoyale
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.

Re: SQL database will not take PHP Form data

Posted: Sat Apr 12, 2008 3:40 am
by visionmaster
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:

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);
[...]
 
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

Re: SQL database will not take PHP Form data

Posted: Sat Apr 12, 2008 9:40 am
by bdlang

Code: Select all

"INSERT INTO [b]'user'[/b] VALUES...
Erm, remove the 'single quotes' wrapping your table name.

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

Posted: Sat Apr 12, 2008 3:39 pm
by latoyale
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?

Re: SQL database will not take PHP Form data

Posted: Sun Apr 13, 2008 10:57 am
by latoyale
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.";