gpt4 book ai didi

php - 想要删除PHP表中数据库中的用户

转载 作者:行者123 更新时间:2023-11-29 08:47:06 26 4
gpt4 key购买 nike

我有一个表,其中填充了数据库中的数据,我希望它旁边有一个按钮,供每个特定用户删除该用户。但我该怎么做呢?

代码如下:

<?php               
include '../includes/db_connect.php';

$sql = "SELECT * FROM `users`";
$result = mysql_query($sql, $connection) or die ("Failed to excecute the query $sql on $connection");
?>
<table border=1>
<tr>
<th>
Access lvl
</th>
<th>
Username
</th>
<th>
Email
</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo "</td><td>";
echo $row['access'];
echo "</td><td>";
echo $row['username'];
echo "</td><td>";
echo $row['email'];
echo "</td></tr>";
}
echo "</table>";
?>

最佳答案

我经常这样做并使用 jQuery。我使用以下内容:

HTML:

<table>
....
<tr>
<td><span class="delete">Delete me!</span></td>
</tr>
</table>

jQuery:

$(document).on("click", ".delete", function(event)
{
var sData = "?id=" + $(this).data("id");
$.ajax({
url: "pages/delete_script.php",
type: "POST",
data: sData,
success: function(sResult)
{
// Process the data you got back from the delete script
// For example removing the row if successfully deleted:
if(sResult == "OK")
{
$(this).closest("tr").remove();
}
},
statusCode:
{
404: function()
{
alert("page not found");
}
}
});
});

on() 用于动态元素(我在该页面上使用 AJAX 执行所有操作)。然后我将需要发送的数据以正确的格式放入并执行 AJAX 请求。在 PHP 文件中可能是这样的:

if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['id'])
{
mysql_query(
sprintf('DELETE FROM `table` WHERE `id` = %d', $_POST['id'])
);
}
}

当然,建议进行更多检查,以确保删除正确的项。

关于php - 想要删除PHP表中数据库中的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12296086/

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