Home > Backend Development > PHP Problem > How to add multiple rows of data in php

How to add multiple rows of data in php

藏色散人
Release: 2023-03-14 19:28:01
Original
2131 people have browsed it

php method to add multiple rows of data: 1. Create a PHP sample file; 2. Connect to the database; 3. Execute multiple SQL statements through the mysqli_multi_query() function and use the INSERT statement to add multiple rows to the data table. Just data.

How to add multiple rows of data in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to add multiple rows of data in php?

mysqli_multi_query() function can be used to execute multiple SQL statements.

The following example adds three new records to the "MyGuests" table:

Example (MySQLi - Object-oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
 
// 创建链接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查链接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
 
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;John&#39;, &#39;Doe&#39;, &#39;john@example.com&#39;);";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;Mary&#39;, &#39;Moe&#39;, &#39;mary@example.com&#39;);";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;Julie&#39;, &#39;Dooley&#39;, &#39;julie@example.com&#39;)";
 
if ($conn->multi_query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
 
$conn->close();
?>
Copy after login

Note Please note that each SQL statement must be separated by semicolons.

Example (MySQLi - process-oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
 
// 创建链接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查链接
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
 
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;John&#39;, &#39;Doe&#39;, &#39;john@example.com&#39;);";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;Mary&#39;, &#39;Moe&#39;, &#39;mary@example.com&#39;);";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;Julie&#39;, &#39;Dooley&#39;, &#39;julie@example.com&#39;)";
 
if (mysqli_multi_query($conn, $sql)) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
 
mysqli_close($conn);
?>
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to add multiple rows of data in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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