This article mainly introduces the PHP + MySQL insert operation. The example analyzes the anti-SQL injection and insert operations skills, for more information, see the example in this article to describe how to implement the PHP + MySQL insert operation. Share it with you for your reference. The details are as follows:
The code is as follows:
INSERT operation
<? Php
If (! Isset ($ _ POST ['submit ']) {
// If no form is submitted, a form is displayed.
?>
<? Php
}
Else
{
// If the form is submitted
// Database connection parameters
$ Host = "localhost ";
$ User = "root ";
$ Pass = "zq19890319 ";
$ Db = "phpdev ";
// Obtain the value in the form, check whether the value in the form complies with the standard, and escape it to prevent SQL injection.
$ Country = empty ($ _ POST ['country'])? Die ("enter the country name "):
Mysql_escape_string ($ _ POST ['country']);
$ Animal = empty ($ _ POST ['Animal '])? Die ("Enter 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 a 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 ());
// The insert record number is displayed after the insert operation is successful.
Echo "the 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 design the php + mysql program.