PHP MySQL insert into a combination of detailed and instance code _mysql

Source: Internet
Author: User
Tags mysql insert php mysql

PHP MySQL insert into a combination of detailed

Ysql INSERT into statement is often used in the actual application of the statement, so the relevant content or a lot of good grasp.

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_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 insensitive to case. Insert into is the same as insert into.

A 1.insert statement can insert multiple sets of values at a time, with each set of values enclosed in a pair of parentheses, separated by commas, as follows:

INSERT INTO ' news ' (Title,body,time) VALUES (' Title 1 ', ' body 1 ', now ()), (' Title 2 ', ' body 2 ', now ());

The 2.Insert Select statement inserts the result of the query into a new table, as the name suggests, consisting of an Insert statement and a Select statement

Insert into News_one (id,title,body,time) select Id,title,body,time from News_two;

Note that the structure of the two tables is identical and the column names can be different.

How to use in PHP

The following 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)
?>

Thank you for reading, I hope to help you, thank you for your support for this site!

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.