Home > Backend Development > PHP Problem > How to delete data and jump to the page in php

How to delete data and jump to the page in php

PHPz
Release: 2023-03-29 14:02:49
Original
561 people have browsed it

In PHP, deleting data and jumping to the page is a common operation. Deleting data typically requires interaction with the database and, upon successful deletion, redirecting the user to another page.

If you want to learn how to delete data and jump pages in PHP, please dive into the following steps.

Step 1: Establish a database connection

Before operating the database, a connection to the database must be established. This can be done with the following code:

$servername = "localhost";  // 数据库服务器名称
$username = "username";     // 数据库用户名
$password = "password";     // 数据库密码
$dbname = "myDB";           // 数据库名称

// 创建数据库连接
$conn = mysqli_connect($servername, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
Copy after login

This will create a connection to the MySQL database. If the connection fails, an error message is output and execution of the script is terminated.

Step 2: Create deletion instructions

Once the database connection is established, you can start creating deletion instructions. Delete instructions are usually expressed in the form of SQL statements and need to be sent to the database to perform the delete operation. Here is an example of a delete instruction:

// 获取要删除的数据
$id = $_GET['id'];

// 创建删除指令
$sql = "DELETE FROM customers WHERE id=$id";

// 执行删除指令
if (mysqli_query($conn, $sql)) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}
Copy after login

The SQL statement here can be adjusted according to your database structure.

Create a delete instruction during the 'delete' operation by obtaining the data to be deleted from the URL parameter $_GET variable. After executing the instruction, it will be checked whether it was executed successfully and the corresponding message will be output.

Step 3: Redirect to another page

Redirecting the user to another page after a successful delete operation is a good user experience. You can use PHP's header function to achieve this purpose.

// 重定向到另一个页面
header('Location: success.php');

// 确保之前没有输出任何内容
exit;
Copy after login

In this example, the user will be redirected to the "success.php" page. Please make sure nothing is output before calling the header function.

The complete code is as follows:

// 建立数据库连接
$servername = "localhost";  // 数据库服务器名称
$username = "username";     // 数据库用户名
$password = "password";     // 数据库密码
$dbname = "myDB";           // 数据库名称

$conn = mysqli_connect($servername, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// 获取要删除的数据
$id = $_GET['id'];

// 创建删除指令
$sql = "DELETE FROM customers WHERE id=$id";

// 执行删除指令
if (mysqli_query($conn, $sql)) {
    // 重定向到另一个页面
    header('Location: success.php');

    // 确保之前没有输出任何内容
    exit;
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}

// 关闭数据库连接
mysqli_close($conn);
Copy after login

It is assumed that the 'delete' operation is performed by passing the 'id' variable through the URL parameter. Then, create a delete instruction and redirect the user to another page upon successful execution.

Summary:

To delete data and jump to the page in PHP, you need to perform the following steps:

1. Establish a database connection

2. Create a delete command And execute

3. Redirect to another page

If you master these steps, you can easily implement the delete operation and jump to the page in your own PHP project.

The above is the detailed content of How to delete data and jump to the page in php. 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