1. Home
  2. Computing & Technology
  3. PHP / MySQL

Storing User Submitted Data and Files in MySQL

By Angela Bradley, About.com

2 of 7

Insert Into - Adding Data from a Form

Next you need to make process.php, the page that our form sends its data to. Here is an example of how to collect this data to post to the MySQL database:

<?
$name=$_POST['name'];
$email=$_POST['email'];
$location=$_POST['location'];
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$location')");
Print "Your information has been successfully added to the database.";
?>

As you can see the first thing we do is assign variables to the data from the previous page. We then just query the database to add this new information.

Of course before we try it we need to make sure the table actually exists. Executing this code should create a table that can be used with our sample files:

CREATE TABLE data (name VARCHAR(30), email VARCHAR(30), location VARCHAR(30));

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. SQL Form - SQL File - Insert Into

©2009 About.com, a part of The New York Times Company.

All rights reserved.