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

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags data example function html mysql mysql tutorial mysql_error mysql_query
Insert data into the database table

INSERT INTO statement is used to add a new record to the database table.

Syntax INSERT INTO table_name VALUES (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: SQL statements are case insensitive. INSERT INTO and insert into the same.

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

example

In the previous section, we created a table named "Person" 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 "Person" table:

<? php $ con = mysql_connect ("localhost", "peter", "abc123"); if ($ con) {die ('Could not connect:'. mysql_error ());} mysql_select_db ("my_db", $ mysql_query ("INSERT INTO person (FirstName, LastName, Age) VALUES ('Glen', 'Griffin', '35')" ',' Quagmire ',' 33 ') "); mysql_close ($ con);?> Insert data from the form into the database

Now, let's create an HTML form that inserts the new record into the "Person" table.

This is the HTML form:

<html> <body> <form action = "insert.php" method = "post"> Firstname: <input type = "text" name = "firstname" /> Lastname: <input type = "text" name = "lastname "/> Age: <input type =" text "name =" age "/> <input type =" submit "/> </ form> </ body> </ html>

When the user clicks the submit button in the HTML form in the example above, the form data is sent to "insert.php". The "insert.php" file connects to the database and retrieves the value from the form via the $ _POST variable. Then, the mysql_query () function executes the INSERT INTO statement, adding a new record to the database table.

Here's 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", $ $ mysql_query (); $ sql = "INSERT INTO person (FirstName, LastName, Age) VALUES ('$ _POST [firstname]', '$ _ POST [lastname]', '$ _ POST [age]')"; $ sql, $ con)) {die ('Error:'. mysql_error ());} echo "1 record added"; mysql_close ($ con)?>
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: 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.