SQL database will not take PHP Form data

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

SQL database will not take PHP Form data

Post 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.
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Re: SQL database will not take PHP Form data

Post 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
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Re: SQL database will not take PHP Form data

Post 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.
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: SQL database will not take PHP Form data

Post 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?
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: SQL database will not take PHP Form data

Post 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.";
Post Reply