PHP form data written to MySQL database code, PHP form MySQL database
Not much nonsense to say, directly to everyone to paste the code, the specific code is as follows:
<?php} else {//if the form//database connection parameter is submitted $host = "LocalHost"; $user = "root"; $pass = "ZQ"; $db = "Phpdev"; Why do you ask hovertree.com//get the values in the form, check that the values in the form conform to the standard, and do the appropriate escaping to prevent SQL injection $country = empty ($_post[' country ')? Die ("Please enter the country name"): mysql_escape_string ($_post[' country ')); $animal = Empty ($_post[' animal ')? Die ("Please enter English name"): mysql_escape_string ($_post[' animal ')); $cname = Empty ($_post[' cname '])? Die ("Please enter Chinese name"): mysql_escape_string ($_post[' cname '); Open database Connection $connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect!"); Select Database mysql_select_db ($db) or Die ("Unable to select database!"); Constructs a SQL query $query = "INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')"; Execute the query $result = mysql_query ($query) or Die ("Error in Query: $query.") Mysql_error ()); After the insert operation succeeds, the record number of the inserted record is displayed echo "record has been inserted, mysql_insert_id () =". mysql_insert_id (); Close the current database connection mysql_close ($connection); }?>
The code above is in a different format and does not know which format is better
<?php} else {//if the form//database connection parameter is submitted $host = "LocalHost"; $user = "root"; $pass = "ZQ"; $db = "Phpdev"; Get the values in the form, check that the values in the form conform to the standard, and do the appropriate escaping to prevent SQL injection $country = empty ($_post[' country ')? Die ("Please enter the country name"): mysql_escape_string ($_post[' country ')); $animal = Empty ($_post[' animal ')? Die ("Please enter English name"): mysql_escape_string ($_post[' animal ')); $cname = Empty ($_post[' cname '])? Die ("Please enter Chinese name"): mysql_escape_string ($_post[' cname '); Open database connection hovertree.com Ask $connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect!"); Select Database mysql_select_db ($db) or Die ("Unable to select database!"); Constructs a SQL query $query = "INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')"; Execute the query $result = mysql_query ($query) or Die ("Error in Query: $query.") Mysql_error ()); After the insert operation succeeds, the record number of the inserted record is displayed echo "record has been inserted, mysql_insert_id () =". mysql_insert_id (); Close the current database connection mysql_close ($connection); }?>
The above is a small series to introduce you to the PHP form data written to MySQL database code, I hope to help you!
http://www.bkjia.com/PHPjc/1133042.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133042.html techarticle PHP form data written to the MySQL database code, PHP form MySQL database nonsense to say, directly to everyone to paste the code, the specific code is as follows: H insert operation/php if (!isset .