PHP+MySQL insert operation example, phpmysql insert example
The example in this article describes the implementation method of PHP+MySQL insert operation. Share it with everyone for your reference. The details are as follows:
Copy code The code is as follows:
Insert operation
If(!isset($_POST['submit'])){
//If no form is submitted, display a form
?>
}
else else
{
//If the form is submitted
//Database connection parameters
$host = "localhost";
$user = "root";
$pass = "zq19890319";
$db = "phpdev";
//Get the value in the form, check whether the value in the form meets the standards, and escape it appropriately 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 an English name"):
Mysql_escape_string($_POST['animal']);
$cname = empty($_POST['cname'])? die("Please enter the 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!");
//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());
//After the insertion operation is successful, 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);
}
?>
I hope this article will be helpful to everyone’s php+mysql program design.
http://www.bkjia.com/PHPjc/945699.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/945699.html
TechArticlePHP+MySQL insert operation example, phpmysql insert operation example This article describes the implementation method of PHP+MySQL insert operation. Share it with everyone for your reference. The details are as follows: Copy the code The code is as follows...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn