gpt4 book ai didi

php - 从数据库中删除用户 - GET 方法更改为 POST 方法

转载 作者:行者123 更新时间:2023-11-29 06:02:50 24 4
gpt4 key购买 nike

我的问题是如何在用户列表页面上的删除按钮上单击操作后将 GET 方法更改为 POST 方法。

在页面 users.php 顶部使用 POST 删除后必须是消息“用户 $name 已删除!”

用户.php

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8"/>
<title>Users</title>
</head>

<body>
<h1>Users List</h1>

<?php
include('db_connect.php');

$db_conn = @new mysqli($host, $db_user, $db_password, $db_name);


if($result=$db_conn->query("SELECT * FROM user ORDER BY id")){
if($result->num_rows > 0){


echo "<table border='1' cellpadding='10'>";
echo "<tr><th>ID</th><th>Name</th></tr>";


while($row=$result->fetch_object()){
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->name."</td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";

}else{
echo "No records";
}
}else{
echo "error: ". $db_conn->error;
}

$db_conn->close();

?>

<br><a href="add.php">Add user</a>

</body>
</html>

删除.php

<?php

require_once 'db_connect.php';


$db_conn = @new mysqli($host, $db_user, $db_password, $db_name);

if (isset($_GET['id']) && is_numeric($_GET['id']))
{

$id = $_GET['id'];


if ($stmt = $db_conn->prepare("DELETE FROM user WHERE id = ? LIMIT 1"))
{
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare SQL statement.";
}
$db_conn->close();


header("Location: users.php");
}
else

{
header("Location: users.php");
}

求助! :)

最佳答案

使用带有隐藏输入和提交按钮而不是链接的 form:

改变:

        echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";

与:

        echo '<td><form action="delete.php" method="POST"><input type="hidden" name="id" value="' . $row->id . '"><input type="submit" name="submit" value="Delete"></form></td>';

& 当然这应该用 PHP 处理:

改变:

if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];

收件人:

if (isset($_POST['id']) && is_numeric($_POST['id']))
{
$id = $_POST['id'];

关于php - 从数据库中删除用户 - GET 方法更改为 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43780208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com