How to Insert Data From a Form in PHP to a MySQL Database

By Sue Smith

HTML forms can be used to capture many different types of user data.
i Hemera Technologies/AbleStock.com/Getty Images

Inserting data into MySQL from a form on a website using PHP is something that developers often do. The task is not complicated, so even if you do not have advanced programming skills you should be able to carry it out with no trouble. The main technologies involved are HTML, PHP and SQL. To implement an SQL insertion on a database from your PHP scripts, you need to observe a few simple steps. You will use two files, one for the HTML form and another to process the data.

Step 1

Create an HTML form within your page as follows:

Insert Some Text:

This is a simplistic example form to demonstrate the principle of capturing and inserting data. Save your page with either ".html" or ".php" extension.

Step 2

Create a PHP script to process your form data. Using the following outline, create a file and save it "insert_data.php" to match the value in your HTML form tag "action" attribute:

//capture the passed data

$inserted_text = $_POST["formtext"];

?>

When the user presses the submit button on the HTML form, the data is captured from any fields in the form and passed to the PHP "insert_data" script in the POST variable. You can therefore access the inserted data from within the PHP script.

Step 3

Make a connection to the MySQL database and insert the data. The detail of your SQL will depend on your own database, but the following outline illustrates the technique:

//connect

mysql_connect("localhost", "db_user", "db_pass");

mysql_select_db("db_name");

//insert

$insert_query = "INSERT INTO tablename (column1) VALUES ('".$inserted_text."')";

$insertion_result = mysql_query($insert_query);

This PHP code takes the captured data passed from the form page and inserts it into a MySQL database as part of an SQL insert statement. Change the script to match the connection details and structure of your own database.

Step 4

Give the user feedback following their data insertion. Using the following outline, output HTML to let the user know whether their data has been processed:

//check whether the data insertion was successful

if(!$insertion_result)

echo "

Sorry! Something went wrong.

";

else

echo "

Thanks! Your form has been processed.

";

You can include within your "insert_data" script whatever other HTML elements you like. Remember to include a valid HTML page structure so that the result will be presented reliably in the user's browser.

Step 5

Upload your HTML page and PHP scripts to your Web server. Browse to the page with the form on it and test it. Check that it carries out the insertion on your database by logging into it and making sure the inserted content is being added to the appropriate table. Also check that the PHP script presents the feedback to the user when the insertion has been carried out. If the insertion does not function correctly, check your syntax and the values you are using for connecting to the database.

×