![](/img/trans.png)
[英]How to delete a record in database using php? I have some of my codes in deleting but it won't delete the record
[英]How to delete a record in your database using php and staying in the same page after deleting?
我有一个贡献者数据库,我想使用 php 从我的数据库中删除其中的一些。 我写了代码,但它给出了错误。 我想我以错误的方式链接了 php 和 html。 一旦我删除我想留在同一页面。 另外我想知道我是否可以使我的代码更安全。 但是,此页面由一个拥有用户名和密码并应输入 OTP 的用户访问。 这是代码:
<?php
/* Delete button */
if(isset($_POST['delete']))
{
$query = "DELETE FROM contributors WHERE id='".$_GET['id']."' ";
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "volunteedbzlfqf");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<table>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['password'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['DOB'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['major'];?></td>
<td><?php echo $row['service'];?></td>
<td><?php echo $row['hours'];?></td>
<td><?php echo $row['hrPrice'];?></td>
<td><button name="modify" id="<?php.$row['id'].?>">#</button></td>
<td><button name="delete" id="<?php.$row['id'].?>">X</button></td>
</tr>
<?php endwhile;?>
</table>
</html>
当 go 里面删除:
if(isset($_POST['delete']))
{
$query = "DELETE FROM contributors WHERE id='".$_GET['id']."' ";
$search_result = filterTable($query);
}
您可以检查用户是否已登录(错误是我知道 URL 并将 url/table/randomIdToDelete 放入 postman 并可以删除您的数据库。
if(isset($_POST['delete']))
{
if(!isset($_SESSION['user']){
return;
}
$query = "DELETE FROM contributors WHERE id='".$_GET['id']."' ";
$search_result = filterTable($query);
}
您可以在 wordpress 中为此使用 ajax。 请参阅此示例链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.