PHP + MySQL insert operation instance

Source: Internet
Author: User
Tags mysql insert
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
// 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.

Related Article

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: 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.