PHP handles addition, modification, deletion and query pages
Overview
With the development of Web technology, more and more websites need to interact with databases. As a commonly used Web programming language, PHP can effectively interact with databases. Among them, processing add, modify, delete, and check operations are the most basic and commonly used.
This article will introduce how to use PHP to handle addition, modification, deletion, and search operations, and give corresponding sample codes.
1. Database connection
In PHP, to interact with the database, you first need to establish a connection with the database. Common databases include MySQL, SQLite, etc. Here, MySQL is taken as an example.
The steps to establish a database connection are as follows:
$conn = mysqli_connect($host, $username, $password, $dbname);
Among them, $host is the host name and $username is the user name, $password is the password, and $dbname is the database name.
if (!$conn) { die("连接失败:" . mysqli_connect_error()); }
If the connection fails, an error message will be output and the program will end.
mysqli_set_charset($conn, "utf8");
2. Insert data
Inserting data is the operation of adding new data to the database. The SQL statement to insert data is generally "INSERT INTO table name (field name 1, field name 2,...) VALUES (value 1, value 2,...)".
The steps to insert data are as follows:
$sql = "INSERT INTO user (name, age, gender) VALUES ('$name', '$age', '$gender')";
Among them, '$name', '$age' , '$gender' is the data that needs to be inserted.
$result = mysqli_query($conn, $sql);
If the execution is successful, $result returns true, otherwise it returns false.
3. Update data
Updating data is to modify the data in the database. The SQL statement to update data is generally "UPDATE table name SET field name 1=value 1, field name 2=value 2...WHERE condition".
The steps to update data are as follows:
$sql = "UPDATE user SET age='$age', gender='$gender' WHERE id='$id'";
Among them, $age and $gender need to be updated. Data, $id is the id value of the data that needs to be updated.
$result = mysqli_query($conn, $sql);
If the execution is successful, $result returns true, otherwise it returns false.
4. Delete data
Deleting data is to delete unnecessary data from the database. The SQL statement to delete data is generally "DELETE FROM table name WHERE condition".
The steps to delete data are as follows:
$sql = "DELETE FROM user WHERE id='$id'";
Among them, $id is the id of the data that needs to be deleted. value.
$result = mysqli_query($conn, $sql);
If the execution is successful, $result returns true, otherwise it returns false.
5. Query data
Querying data is to obtain the required data from the database. The SQL statement for querying data is generally "SELECT field name 1, field name 2...FROM table name WHERE condition".
The steps to query data are as follows:
$sql = "SELECT * FROM user WHERE gender='男'";
Among them, * means querying all fields, gender=' Male' is the query condition.
$result = mysqli_query($conn, $sql);
If the execution is successful, $result will return a result set.
while($row = mysqli_fetch_assoc($result)) { $name = $row["name"]; $age = $row["age"]; $gender = $row["gender"]; echo "姓名:$name,年龄:$age,性别:$gender<br>"; }
This code will parse each row of data in the result set into an associative array, and output the corresponding name, age, gender and other information .
6. Complete sample code
The following is a complete sample code for processing add, modify, delete, and check operations:
// 连接数据库 $conn = mysqli_connect($host, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); if (!$conn) { die("连接失败:" . mysqli_connect_error()); } // 插入数据 $name = "张三"; $age = "20"; $gender = "男"; $sql = "INSERT INTO user (name, age, gender) VALUES ('$name', '$age', '$gender')"; $result = mysqli_query($conn, $sql); if ($result === true) { echo "插入成功
"; } else { echo "Error:" . mysqli_error($conn) . "
"; } // 更新数据 $age = "22"; $gender = "女"; $id = 1; $sql = "UPDATE user SET age='$age', gender='$gender' WHERE id='$id'"; $result = mysqli_query($conn, $sql); if ($result === true) { echo "更新成功
"; } else { echo "Error:" . mysqli_error($conn) . "
"; } // 删除数据 $id = 2; $sql = "DELETE FROM user WHERE id='$id'"; $result = mysqli_query($conn, $sql); if ($result === true) { echo "删除成功
"; } else { echo "Error:" . mysqli_error($conn) . "
"; } // 查询数据 $sql = "SELECT * FROM user WHERE gender='男'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $name = $row["name"]; $age = $row["age"]; $gender = $row["gender"]; echo "姓名:$name,年龄:$age,性别:$gender
"; } } else { echo "没有符合条件的数据
"; } // 关闭数据库连接 mysqli_close($conn);
7. Summary
Processing Add, modify, delete, and query operations are the basic and common operations for interacting with the database using PHP. Through the introduction of this article, I believe that readers have already understood how to use PHP to handle addition, modification, deletion, and search operations, and also mastered the corresponding sample code.
The above is the detailed content of How to handle add, modify, delete and check pages in php. For more information, please follow other related articles on the PHP Chinese website!