PHP is a scripting language commonly used to write dynamic interactive web pages. Unlike static web pages, the content of dynamic web pages can be updated at any time. By interacting with the database, real-time modifications to the website content can be achieved. This article will introduce how to use PHP to modify the contents of the database in a web page.
First, you need to connect to the database in the web page. In PHP, you can use libraries such as mysqli or PDO to connect to the database. The following takes mysqli as an example:
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ?>
In the above code, $servername represents the name of the database server, $username represents the username to connect to the database, $password represents the password to connect to the database, and $dbname represents the database to be connected. name. If the connection fails, a connection failure message will be output.
After the connection is successful, the contents of the database can be modified in the web page through PHP. Here are some commonly used methods.
1. Update data
If you want to update the data in the database, you can use the UPDATE statement. For example, to update the name field of the content with id 1 in the table to "zhangsan", you can use the following code:
$sql = "UPDATE myTable SET name='zhangsan' WHERE id=1"; if ($conn->query($sql) === TRUE) { echo "记录更新成功"; } else { echo "记录更新失败: " . $conn->error; }
In the above code, $sql represents the SQL statement to be executed. If the execution is successful, "Record Update Successful" is output, otherwise "Record Update Failed" is output.
2. Insert data
If you want to insert data into the database, you can use the INSERT statement. For example, to insert a record into the table, you can use the following code:
$sql = "INSERT INTO myTable (name, age, gender) VALUES ('zhangsan', 20, '男')"; if ($conn->query($sql) === TRUE) { echo "新记录插入成功"; } else { echo "新记录插入失败: " . $conn->error; }
In the above code, $sql represents the SQL statement to be executed. If the execution is successful, it will output "new record insertion successfully", otherwise it will output "new record insertion failed".
3. Delete data
If you want to delete data from the database, you can use the DELETE statement. For example, to delete the record with id 1 from the table, you can use the following code:
$sql = "DELETE FROM myTable WHERE id=1"; if ($conn->query($sql) === TRUE) { echo "记录删除成功"; } else { echo "记录删除失败: " . $conn->error; }
In the above code, $sql represents the SQL statement to be executed. If the execution is successful, "Record Deletion Successful" is output, otherwise "Record Deletion Failed" is output.
4. Query data
If you want to query data from the database, you can use the SELECT statement. For example, to query all records in the table, you can use the following code:
$sql = "SELECT * FROM myTable"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Age: " . $row["age"] . " - Gender: " . $row["gender"] . "<br>"; } } else { echo "0 个结果"; }
In the above code, $sql represents the SQL statement to be executed. $result represents the result set returned by executing the SQL statement. If there is data in the result set, use a while loop to output all the data.
The above are several common ways to use PHP to modify the content of the database in web pages. It should be noted that when modifying the contents of the database, you should pay attention to the following points:
In short, when using PHP to modify the content of the database, you must be careful to prevent problems such as misoperation or data tampering.
The above is the detailed content of How to use PHP to modify the contents of the database in a web page. For more information, please follow other related articles on the PHP Chinese website!