Home > Backend Development > PHP Problem > How to use PHP to add data to the database

How to use PHP to add data to the database

PHPz
Release: 2023-04-04 18:34:02
Original
1279 people have browsed it

In PHP programming, it is often necessary to use a database to store and manage data. In practical applications, it is often necessary to add data to the database. This article will introduce how to use PHP to implement the function of adding data to the database.

1. Connect to the database

Before using PHP to operate the database, you first need to connect to the database. You can use extension libraries such as mysqli or PDO to achieve database connection. The specific operations are as follows:

Use the mysqli extension library to connect to the database:

$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
    die("连接失败: " . $mysqli->connect_error);
}
Copy after login

Use the PDO extension library to connect to the database:

$dsn = "mysql:host=localhost;dbname=database";
$username = "username";
$password = "password";
try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("连接失败:" . $e->getMessage());
}
Copy after login

2. Prepare SQL statements

After successfully connecting to the database, you need to prepare the SQL statements to be executed. SQL statements usually include keywords such as INSERT INTO and VALUES, which are used to specify the data and data tables to be added. For example:

INSERT INTO student(name, age) VALUES ('张三', 18);
Copy after login

In the SQL statement, you need to specify the data table to be added, the fields to be added, and the corresponding values. This information needs to be set according to actual needs.

3. Execute the SQL statement

After preparing the SQL statement, you can execute it and add the data to the database. In both mysqli and PDO extension libraries, you can use the exec function to execute SQL statements. The specific operations are as follows:

Use the mysqli extension library to execute SQL statements:

$sql = "INSERT INTO student(name, age) VALUES ('张三', 18)";
if ($mysqli->query($sql) === TRUE) {
    echo "数据插入成功";
} else {
    echo "数据插入失败: " . $mysqli->error;
}
Copy after login

Use the PDO extension library to execute SQL statements:

$sql = "INSERT INTO student(name, age) VALUES ('张三', 18)";
try {
    $pdo->exec($sql);
    echo "数据插入成功";
} catch (PDOException $e) {
    echo "数据插入失败: " . $e->getMessage();
}
Copy after login

4. Complete sample code

// 使用mysqli扩展库连接数据库
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
    die("连接失败: " . $mysqli->connect_error);
}

// 准备要执行的SQL语句
$sql = "INSERT INTO student(name, age) VALUES ('张三', 18)";

// 执行SQL语句,并判断是否执行成功
if ($mysqli->query($sql) === TRUE) {
    echo "数据插入成功";
} else {
    echo "数据插入失败: " . $mysqli->error;
}

// 使用PDO扩展库连接数据库
$dsn = "mysql:host=localhost;dbname=database";
$username = "username";
$password = "password";
try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("连接失败:" . $e->getMessage());
}

// 准备要执行的SQL语句
$sql = "INSERT INTO student(name, age) VALUES ('张三', 18)";

// 执行SQL语句,并判断是否执行成功
try {
    $pdo->exec($sql);
    echo "数据插入成功";
} catch (PDOException $e) {
    echo "数据插入失败: " . $e->getMessage();
}
Copy after login

5. Summary

This article introduces how to use PHP to implement the function of adding data to the database, which mainly includes steps such as connecting to the database, preparing SQL statements, and executing SQL statements. Once you master this knowledge, you can easily add data to the database and implement more practical functions.

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

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