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.

This is a simplistic example form to demonstrate the principle of capturing and inserting data. Save your page with either “.html” or “.php” extension. $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. //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. //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. Tips Warnings Writer Bio

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