Php+mysql insert operation Instance _php tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.