The example of this article describes the implementation of Php+mysql insert operation. Share to everyone for your reference. Specifically as follows:
Copy Code code as follows:
<?php
if (!isset ($_post[' submit ')) {
If there is no form submission, display a form
?>
<form action= "" method= "POST" >
Country: <input type= "text" name= "Country"/>
Animal Name (English): <input type= "text" name= "Animal"/>
Animal Name (Chinese): <input type= "text" name= "CNAME"/>
<input type= "Submit" name= "Submission" value= "submitting form"/>
</form>
<?php
}
Else
{
If you submit a form
Database connection Parameters
$host = "localhost";
$user = "root";
$pass = "zq19890319";
$db = "Phpdev";
Gets the values in the form, checks that the values in the form conform to standards, and makes appropriate escape 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!");
Construct an 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 "Record has been inserted, mysql_insert_id () =". mysql_insert_id ();
Close the current database connection
Mysql_close ($connection);
}
?>
I hope this article will help you with the Php+mysql program design.