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.
Create an HTML form within your page as follows:
<form action="insert_data.php" method="post">
Insert Some Text: <input type="text" name="formtext" />
<input type="submit" />
This is a simplistic example form to demonstrate the principle of capturing and inserting data. Save your page with either ".html" or ".php" extension.
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.
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:
mysql_connect("localhost", "db_user", "db_pass");
$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.
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
echo "<p>Sorry! Something went wrong.</p>";
echo "<p>Thanks! Your form has been processed.</p>";
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.
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.
Tips & Warnings
- HTML forms can sometimes be attacked by spam, which can fill up your database with nonsense values. Using a Captcha is a common defense.
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images
How to Save Dreamweaver Web Form Data Into a Database
How to Save Dreamweaver Web Form Data Into a ... Working in Code View; Webb Blazonry: MySQL PHP ... Adobe Dreamweaver is...
How to Save TinyMCE Data to MySQL PHP
How to Save TinyMCE Data to MySQL PHP. ... Insert the data into the database. The "insert" statement adds a new record...
How to Transfer Data in Web Form to a Database
How to Transfer Data in Web Form to a Database. ... How to Insert Data From a Form in PHP to a...
How to Insert a Decimal in PHP MySQL
PHP provides many standard functions for handling MySQL databases. This means that inserting data into a MySQL database table using a PHP...