Home  >  Article  >  Backend Development  >  Examples of how to use PHP to add data to a MySQL data table

Examples of how to use PHP to add data to a MySQL data table

PHPz
PHPzOriginal
2023-04-03 17:55:091398browse

In PHP, adding data to a database table is a very common task, as many web applications require adding information to the database at runtime.

This article will introduce how to use PHP to add data to a MySQL database table. In this article, we will use the MySQLi extension library as a sample database.

  1. Connect to the database

First, you need to connect to the MySQL database. You can use code like this:

// 设置数据库连接信息
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

Here, you need to replace "username" and "password" with your MySQL username and password, and "database_name" with the name of the database you want to connect to.

  1. Inserting Data

After setting up the connection, you can add new data to the database table. In the following example, we will add a new user to the table named "users":

// 设置插入语句
$sql = "INSERT INTO users (username, email, password) VALUES ('JohnDoe', 'johndoe@example.com', 'somepassword')";

// 执行插入语句
if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

In this example, we use the INSERT INTO statement to add a new user to the table named "users" user. We added user information to the "username", "email" and "password" columns respectively.

Use the $conn->query() method to execute the query and output a success or error message based on the query results.

  1. Insert multiple pieces of data

In the insert statement, we can also use a single query to insert multiple records at once. This can be done by mapping multiple values ​​to multiple INSERT INTO clauses in a single query, for example:

// 批量插入语句示例
$sql = "INSERT INTO users (username, email, password) VALUES ('JohnDoe', 'johndoe@example.com', 'somepassword'),
        ('JaneDoe', 'janedoe@example.com', 'someotherpassword'),
        ('BobSmith', 'bobsmith@example.com', 'yetanotherpassword')";

// 执行批量插入语句
if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

In this example, we use a bracket to enclose three values, which are The "username", "email" and "password" of the new user. Separate each value with a comma and insert the list of query values ​​at the end of the entire query.

  1. Complete the insert operation

After completing the insert operation, we need to close the connection to the database. The following code can be used:

// 关闭数据库连接
$conn->close();

By combining all queries, you can now add new data to a MySQL database table in PHP.

Summary

In this article, we introduced how to use PHP to access a MySQL database and add data to a table. With these examples, you can now start using the MYSQLi extended insert operation to dynamically insert data.

The above is the detailed content of Examples of how to use PHP to add data to a MySQL data table. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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