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