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!