PHP form data written to MySQL code, PHP form written to MySQL
Insert operation
Phpif(!isset($_post[' Submit '])){ //If there is no form submission, a form is displayed?>
PHP}Else { //if the form//database connection parameters are submitted $host= "localhost"; $user= "Root"; $pass= "zq19890319"; $db= "Phpdev"; //what to ask hovertree.com//Get the values in the form, check whether the values in the form conform to the standards, and do the appropriate escaping to prevent SQL injection $country=Empty($_post[' Country '])? die("Please enter 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 a 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()); //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); } ?>
Reference: http://www.cnblogs.com/roucheng/p/phpmysql.html
The above code in a different format, do not know which format is better
Insert operation
-
- if (!isset ($_post[' Submit '])) {
- //If there is no form submission, a form is displayed
- ?>
-
- }
- Else
- {
- //If a form is submitted
- //Database connection Parameters
- $host = "localhost" ;
- $user = "Root" ;
- $pass = "zq19890319" ;
- $db = "Phpdev" ;
- //Get the values in the form, check whether the values in the form conform to the standards, and do the appropriate escaping to prevent SQL injection
- $country = Empty ($_post[' country '])? die ("Please enter 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! " );
- //construct 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 ());
- when the insert operation succeeds, the record number of the inserted record is displayed
- Echo "The record has been inserted, mysql_insert_id () =". mysql_insert_id ();
- //Close the current database connection
- Mysql_close ($connection);
- }
- ?>
http://www.bkjia.com/PHPjc/1130484.html www.bkjia.com true http://www.bkjia.com/PHPjc/1130484.html techarticle PHP form data written to MySQL code, PHP form written to MySQL H1 insert operation/h1? php if (! isset ($_post [' submit '])) {//If there is no form submission, show a form? Form action= ". ..