How can data be added to MySQL using PHP?
To add data to MySQL, you can connect to the database and perform insert operations using either the MySQLi extension or PDO extension in PHP. Here is an example code using the MySQLi extension:
// 连接到MySQL数据库
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败:" . $conn->connect_error);
}
// 准备插入语句
$sql = "INSERT INTO your_table (column1, column2, column3) VALUES ('value1', 'value2', 'value3')";
// 执行插入语句
if ($conn->query($sql) === TRUE) {
echo "数据插入成功";
} else {
echo "数据插入失败:" . $conn->error;
}
// 关闭数据库连接
$conn->close();
Please replace the database connection information, table name, column names, and values to be inserted based on the actual situation.