View Full Version : submit form data into a database.

06-21-2009, 11:04 PM
i have looked at several tutorials and none of them helped. i know this is a lot to ask, but can someone give me the script and instructions to how to submit my form data into my sql database with php?


database colums-- firstname, lastname, username, password, email, state

those fields are also in my form- the text boxes for each of those fields are named the same thing BUT there is a "txt" added to the end of it. ex- firstnametxt, lastnametxt...etc.

all of them is NOT null. can someone give me the script and INSTRUCTIONS for submitting the form info to my database? thx, i know it is a lot to ask, but since im a beginner with PHP, i am very eager to learn this. :)

07-11-2009, 05:07 PM
The anyexample website has some useful functions for sql which makes writing queries in php easier. You then need to use the sql documentation to learn how to use the INSERT operation.

I would suggest using sql Manager Lite because it lets you test your sql queries without php. That way you can tell if your problem is in php or your sql query.

07-13-2009, 02:06 PM
See if this helps:

$con = mysql_connect("(database_name).mysql.anyhost.com","yourDatabaseUsername","yourDatabasePassword"); //Replace with your actual MySQL DB Username and Password
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("yourDatabaseName", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO form_data (name,email) VALUES ('$name','$email')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
echo "The form data was successfully added to your database.";

If testing locally, replace "connection" above with "localhost" and make sure you set the right variables. If adding more, folloe the ones there already.

07-13-2009, 02:07 PM
"values" rather.