php+mysql implements data insertion based on the Insert Into statement

墨辰丷
Release: 2023-03-30 11:42:01
Original
2729 people have browsed it

This article mainly introduces php mysql to implement data insertion based on the Insert Into statement. Interested friends can refer to it. I hope it will be helpful to everyone.

INSERT INTO statement is used to insert new records into the database table.

Insert data into the database table

The INSERT INTO statement is used to add new records to the database table.

Syntax:

INSERT INTO table_name
VALUES (value1, value2,....)
Copy after login

You can also specify the column in which you want to insert data:

INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
Copy after login

Note: SQL statements are not case-sensitive. INSERT INTO is the same as insert into.

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

Example:

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

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
 die(&#39;Could not connect: &#39; . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
VALUES (&#39;Peter&#39;, &#39;Griffin&#39;, &#39;35&#39;)");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
VALUES (&#39;Glenn&#39;, &#39;Quagmire&#39;, &#39;33&#39;)");
mysql_close($con);
?>
Copy after login

Insert the data from the form into the database:

Now, we create an HTML form that can insert the new records Insert into the "Persons" table.
Here is this 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>
Copy after login

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 values ​​from the form through the $_POST variable. Then, the mysql_query() function executes the INSERT INTO statement, and a new record is added to the database table.

The following is the code of the "insert.php" page:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
 die(&#39;Could not connect: &#39; . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
(&#39;$_POST[firstname]&#39;,&#39;$_POST[lastname]&#39;,&#39;$_POST[age]&#39;)";
if (!mysql_query($sql,$con))
{
 die(&#39;Error: &#39; . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study .

Related recommendations:

PHP radix sorting method

Detailed explanation of PHP variables and dynamic string insertion into variables (Case)

PHP implements login verification code function

The above is the detailed content of php+mysql implements data insertion based on the Insert Into statement. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template