Php+mysql data insertion based on INSERT INTO statement

Source: Internet
Author: User
This article mainly introduces php+mysql based on INSERT INTO statement implementation of data insertion, interested in the friend's reference, I hope to be helpful to everyone.

The INSERT INTO statement is used to insert a new record into the database table.

Inserting data into a database table

The INSERT into statement is used to add a new record to a database table.

Grammar:

INSERT into Table_namevalues (value1, value2,....)

You can also specify the columns in which you want to insert data:

INSERT into table_name (column1, Column2,...) VALUES (value1, value2,....)

Note: The SQL statement is not case sensitive. Insert into is the same as insert into.

In order for PHP to execute the statement, we must use the mysql_query () function. This function is used to send a query or command to a MySQL connection.

Example:

In the previous chapters, we created a table named "Persons" with three columns: "Firstname", "Lastname", and "age". We will use the same table in this example. The following example adds two new records to the "Persons" table:

<?php$con = mysql_connect ("localhost", "Peter", "abc123"), if (! $con) {die (' Could not connect: '. Mysql_error ());} mysql_select_db ("my_db", $con); mysql_query ("INSERT into Persons (FirstName, LastName, age) VALUES (' Peter ', ' Griffin ', ' mysql_query ("INSERT into Persons (FirstName, LastName, and age) VALUES (' Glenn ', ' quagmire ', '") "); Mysql_close ($ Con);? >

Insert data from the form into the database:

Now, let's create an HTML form that will insert a new record into the "Persons" table.
This is the HTML form:


When the user clicks the Submit button in the HTML form in the previous example, the form data is sent to "insert.php". The "insert.php" file connects to the database and retrieves the value from the form through the $_post variable. The mysql_query () function then executes the INSERT into statement, and a new record is added to the database table.

Here is the code for the "insert.php" page:

<?php$con = mysql_connect ("localhost", "Peter", "abc123"), if (! $con) {die (' Could not connect: '. Mysql_error ());} mysql_select_db ("my_db", $con); $sql = "INSERT into Persons (FirstName, LastName, age) VALUES (' $_post[firstname] ', ' $_ Post[lastname] ', ' $_post[age] ') "if (!mysql_query ($sql, $con)) {die (' Error: '. mysql_error ());} echo "1 record added"; Mysql_close ($con)?>

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

How PHP cardinality is sorted

PHP variable description and string dynamic Insert variable (case)

PHP Implementation Login Verification code function

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.