12<?PHP3 if(!isset ($_post['Submit'])){ 4 //If there is no form submission, a form is displayed5?>6<form action=""Method="Post">7Country: <input type="text"Name="Country"/>8Animal Name (English): <input type="text"Name="Animal"/>9Animal Name (Chinese): <input type="text"Name="CNAME"/>Ten<input type="Submit"Name="Submit"Value="Submit Form"/> One</form> A<?PHP - } - Else the { - //If you submit the form - //Database Connection Parameters -$host ="localhost"; +$user ="Root"; -$pass ="zq19890319"; +$db ="Phpdev"; A at //how to ask Hovertree.com - //get the values in the form, check that the values in the form conform to the criteria, and do the appropriate escaping to prevent SQL injection -$country = Empty ($_post['Country'])? Die"Please enter a country name"): -Mysql_escape_string ($_post['Country']); -$animal = Empty ($_post['Animal'])? Die"Please enter the English name"): -Mysql_escape_string ($_post['Animal']); in$cname = Empty ($_post['CNAME'])? Die"Please enter Chinese name"): -Mysql_escape_string ($_post['CNAME']); to + //Open a database connection -$connection = mysql_connect ($host, $user, $pass) or Die ("Unable to connect!"); the * //Select Database $mysql_select_db ($db) or Die ("Unable to select database!"); Panax Notoginseng - //constructs a SQL query the$query ="INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')"; + A //Execute the query the$result = mysql_query ($query) or Die ("Error in query: $query.". Mysql_error ()); + - //when the insert operation succeeds, displays the record number of the inserted record $Echo"The record has been inserted, mysql_insert_id () =". mysql_insert_id (); $ - //Close the current database connection - mysql_close ($connection); the } -?>
Reference: http://www.cnblogs.com/roucheng/p/phpmysql.html
The above code in a different format, do not know which format is better
12<?PHP3 if(!isset ($_post['Submit'])){ 4 //If there is no form submission, a form is displayed5?>6<form action=""Method="Post">7Country: <input type="text"Name="Country"/>8Animal Name (English): <input type="text"Name="Animal"/>9Animal Name (Chinese): <input type="text"Name="CNAME"/>Ten<input type="Submit"Name="Submit"Value="Submit Form"/> One</form> A<?PHP - } - Else the { - //If you submit the form - //Database Connection Parameters -$host ="localhost"; +$user ="Root"; -$pass ="zq19890319"; +$db ="Phpdev"; A at //get the values in the form, check that the values in the form conform to the criteria, and do the appropriate escaping to prevent SQL injection -$country = Empty ($_post['Country'])? Die"Please enter a country name"): -Mysql_escape_string ($_post['Country']); -$animal = Empty ($_post['Animal'])? Die"Please enter the English name"): -Mysql_escape_string ($_post['Animal']); -$cname = Empty ($_post['CNAME'])? Die"Please enter Chinese name"): inMysql_escape_string ($_post['CNAME']); - to //Open database connection hovertree.com asked +$connection = mysql_connect ($host, $user, $pass) or Die ("Unable to connect!"); - the //Select Database *mysql_select_db ($db) or Die ("Unable to select database!"); $ Panax Notoginseng //constructs a SQL query -$query ="INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')"; the + //Execute the query A$result = mysql_query ($query) or Die ("Error in query: $query.". Mysql_error ()); the + //when the insert operation succeeds, displays the record number of the inserted record -Echo"The record has been inserted, mysql_insert_id () =". mysql_insert_id (); $ $ //Close the current database connection - mysql_close ($connection); - } the?>
PHP form data written to MySQL code