This article mainly introduces the related materials for writing PHP form data into the MySQL database. it is of great reference value. if you need it, you can refer to it and paste the code directly, the code is as follows:
<? Php} else {// if the form is submitted // database connection parameter $ host = "localhost"; $ user = "root"; $ pass = "zq "; $ db = "phpdev"; // why do I ask hovertree.com//obtain the value in the table list, check the value in the table list to conform to the standard, and make it right to prevent sqlinjection $ country = empty ($ _ POST ['country'])? Die ("enter country name"): mysql_escape_string ($ _ POST ['country']); $ animal = empty ($ _ POST ['Animal '])? Die ("Enter the English name"): mysql_escape_string ($ _ POST ['Animal ']); $ cname = empty ($ _ POST ['cname'])? Die ("Enter Chinese name"): mysql_escape_string ($ _ POST ['cname']); // open the database connection $ connection = mysql_connect ($ host, $ user, $ pass) or die ("Unable to connect! "); // Select the database mysql_select_db ($ db) or die (" Unable to select database! "); // Construct an SQL query $ query =" INSERT INTO symbols (country, animal, cname) VALUE ('$ country', '$ animal', '$ cname ') "; // execute this query $ result = mysql_query ($ query) or die (" Error in query: $ query. ". mysql_error (); // after the insert operation is successful, the record number echo "the record has been inserted, mysql_insert_id () = ". mysql_insert_id (); // close the current database connection mysql_close ($ connection) ;}?>
The above code uses different formats. I don't know which format is better.
<? Php} else {// if the form is submitted // database connection parameter $ host = "localhost"; $ user = "root"; $ pass = "zq "; $ db = "phpdev"; // obtain the value in the form, check whether the value in the form complies with the standard, and perform proper escape, prevent SQL injection $ country = empty ($ _ POST ['country'])? Die ("enter country name"): mysql_escape_string ($ _ POST ['country']); $ animal = empty ($ _ POST ['Animal '])? Die ("Enter the English name"): mysql_escape_string ($ _ POST ['Animal ']); $ cname = empty ($ _ POST ['cname'])? Die ("Enter Chinese name"): mysql_escape_string ($ _ POST ['cname']); // why does it ask $ connection = mysql_connect ($ host, $ user, $ pass) or die ("Unable to connect! "); // Select the database mysql_select_db ($ db) or die (" Unable to select database! "); // Construct an SQL query $ query =" INSERT INTO symbols (country, animal, cname) VALUE ('$ country', '$ animal', '$ cname ') "; // execute this query $ result = mysql_query ($ query) or die (" Error in query: $ query. ". mysql_error (); // after the insert operation is successful, the record number echo "the record has been inserted, mysql_insert_id () = ". mysql_insert_id (); // close the current database connection mysql_close ($ connection) ;}?>
The above is the code for writing PHP form data into the MySQL database. I hope it will help you!