Php mysql insert into combines detailed explanation and instance code, mysqlinsert

Source: Internet
Author: User
Tags mysql insert php mysql

Php mysql insert into combines detailed explanation and instance code, mysqlinsert

Php mysql insert

YSQL insert into statements are frequently used in practical applications. Therefore, it is better to know more about the related content.

Insert data to a database table
The insert into statement is used to add a new record to a database table.

Syntax

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:SQL statements are case insensitive. Insert into is the same as insert.

1. Multiple groups of values can be inserted in an insert statement at a time. Each group of values is enclosed by a pair of parentheses and separated by commas, as shown below:

Insert into 'News' (title, body, time) values ('title 1', 'body 1', now (), ('title 2', 'body 2 ', now ());

2. Insert Select statement: insert the query result to a new table. As the name suggests, it consists 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 two tables have the same structure and different column names.

How to use in php

The code for the "insert. php" page is as follows:

<?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 this article. I hope it will help you. Thank you for your support for this site!

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.