With the rapid development of the Internet, Web development has become an indispensable part of the Internet industry. As an efficient, fast, cross-platform language, PHP has been widely used in the field of web development. In daily development, we often use some common operations, such as adding, deleting, modifying, checking, etc. Among them, deletion is a relatively common operation, but if there is no prompt, accidental deletion of data will still happen frequently. So, how to implement the confirmation prompt during deletion operation in PHP? Next, this article will introduce it to you in detail.
1. DELETE button in HTML
To implement the confirmation prompt during the delete operation, we first need to define a button of type DELETE in HTML according to the HTML specifications. As shown below:
<button type="button" onclick="if(confirm('确认删除吗?')){ location.href='delete.php?id=1';}">删除</button>
Among them, the confirm() method is used to pop up the confirmation dialog box. If the user clicks the "Confirm" button, it returns true; otherwise, it returns false. If the user clicks the "Confirm" button, the following statement will be executed, that is, it will jump to the delete.php page, and pass the parameter id=1 in the URL to delete the specified data.
2. DELETE operation in PHP
With the DELETE button in HTML, we also need to use PHP in the background to implement the corresponding DELETE operation to delete the specified data. Generally speaking, we need to establish a database connection first, and write corresponding SQL statements based on the actual conditions of the database and tables. The following is a sample code:
<?php // 创建连接 $conn = new mysqli("servername", "username", "password", "dbname"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取id参数 $id = $_GET['id']; // SQL语句 $sql = "DELETE FROM `table_name` WHERE `id`='{$id}'"; if ($conn->query($sql) === TRUE) { echo "删除成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
In this sample code, we obtain the parameter id on the URL through GET, then construct the corresponding SQL statement based on the parameter value, and perform the DELETE operation. If the operation is successful, "Deletion Successful" will be output; otherwise, the corresponding error message will be output.
3. Complete sample code
With the above two steps, we can implement the confirmation prompt during the deletion operation. The following is the complete sample code:
PHP删除时提示确认 <button type="button" onclick="if(confirm('确认删除吗?')){ location.href='delete.php?id=1';}">删除</button>
<?php // 创建连接 $conn = new mysqli("servername", "username", "password", "dbname"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取id参数 $id = $_GET['id']; // SQL语句 $sql = "DELETE FROM `table_name` WHERE `id`='{$id}'"; if ($conn->query($sql) === TRUE) { echo "删除成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
Through the combination of the above codes, when we click the delete button on the front-end page, an inquiry box will pop up asking "Confirm deletion?"; if the "Confirm" button is clicked, It will jump to the delete.php page in the background to perform the DELETE operation, thus realizing the function of deleting the specified data.
The above is a detailed introduction to the confirmation prompt method during PHP deletion operation provided by this article. I hope it will be helpful for everyone to learn PHP development.
The above is the detailed content of How to use php to implement the confirmation prompt function when deleting. For more information, please follow other related articles on the PHP Chinese website!