How to Insert Data From a Form in PHP to a MySQL Databaseby Sue Smith
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: <html> <head></head> <body> <form action="insert_data.php" method="post"> Insert Some Text: <input type="text" name="formtext" /> <input type="submit" /> </form> </body> </html> 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: <?php //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: //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.
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 "<p>Sorry! Something went wrong.</p>"; else 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.
- close 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_camera Hemera Technologies/AbleStock.com/Getty Images