How to insert records to the database in PHP + MYSQL

Source: Internet
Author: User
The "INSERTINTO" Statement inserts a new record into a database table. Insert "INSERTINTO" into a database table to insert a new record into a database table. Syntax: INSERTINTOtable_nameVALUES (value1, value2 ,...) You can insert data into a specified column,

The insert into statement inserts a new record INTO a database table. Insert into is used to INSERT a new record INTO a database table. Syntax insert into table_name VALUES (value1, value2 ,...) You can insert data into a specified column,

The insert into statement inserts a new record INTO a database table.

Insert data into a database table

"Insert into" is used to INSERT a new record INTO a database table.

Syntax

Insert into table_name

VALUES (value1, value2 ,...)

You can insert data into the specified column as follows:

Insert into table_name (column1, column2 ,...)

VALUES (value1, value2 ,...)

Note: The SQL statement is a "case-insensitive" Statement (which does not distinguish between uppercase and lowercase letters), that is, "INSERT INTO" and "insert into" are the same.

To create a database in PHP, we need to use the above statement in the mysql_query () function. This function is used to send requests and commands for establishing a MySQL database connection.

Case

In the previous chapter, we created a table named "Person", which contains three columns: "Firstname", "Lastname", and "Age ". In the following case, we will use the same table and add two new records to it:

$ Con = mysql_connect ("localhost", "peter", "abc123 ");

If (! $ Con)

{

Die ('could not connect: '. mysql_error ());

} Mysql_select_db ("my_db", $ con); mysql_query ("insert into person (FirstName, LastName, Age)

VALUES ('Peter ', 'grigin', '35') "); mysql_query (" insert into person (FirstName, LastName, Age)

VALUES ('glenn', 'quagmire', '33') "); mysql_close ($ con );

?>

Insert data from a table into the database

Now we will create an HTML form with which we can add new records to the "Person" table.

The following shows the HTML form:

In the above case, when a user clicks the "submit" button in the HTML form, the data in the form will be sent to "insert. php ". "Insert. the php file is connected to the database and the data in the form is obtained using the PHP $ _ POST variable. In this case, the mysql_query () function executes the insert into statement, A new record is added to the database form.

Code for the next interview "insert. php" Page:

$ Con = mysql_connect ("localhost", "peter", "abc123 ");

If (! $ Con)

{

Die ('could not connect: '. mysql_error ());

} Mysql_select_db ("my_db", $ con); $ SQL = "INSERT INTO person (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)

?>

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.