通过php表中的delete按钮从数据库中删除行

时间:2022-09-26 11:01:15

I'm trying to make a little control panel so I can add and delete rows in a database. I've already made the adding part work but I'm having some trouble with the deleting.

我正在尝试创建一个小的控制面板,这样我就可以在数据库中添加和删除行。我已经完成了添加部分,但是我在删除时遇到了一些麻烦。

The page shows every row in a table and I've added a delete button behind it.

这个页面显示了表中的每一行,我在它后面添加了一个delete按钮。

This are the codes I have

这是我的密码

This is the code that makes the table:

这是制作表格的代码:

<table class='col-sm-6 fixed_headers'>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Email</th>
</tr>
</thead>
<tbody style='height:250px; overflow:auto;'>";

while($row = mysqli_fetch_array($result))
{
 "<tr class=". $row['ID'] . ">";
 echo "<td>" . $row['uname'] . "</td>";
 echo "<td>" . $row['pass'] . "</td>";
 echo "<td>" . $row['email'] . "</td>";
 echo "<td><input type='submit' class='button' id=".$row['ID'] . " value='del'></td>";
 echo "</tr>";
 }
echo "</tbody>
 </table>";
?>  

Here is the ajax code:

以下是ajax代码:

<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(){
    var btnID = $(this).id;


  jQuery.ajax({
        type: "POST",
        url: "php/delete.php",
        data:  {id :btnID}
    }).done(function(result){
    if(result== "succes"){
        $('#error').remove();
        $('#succes').html("Het account is verwijderd. ");

    }else {
        $('#error').empty();
        $('#error').html("Er is een fout opgetreden.<br/> Probeer opnieuw! ");
    }
    });
  });

});
</script>

and here is the delete.php:

这里是delete。php:

$id =  mysql_real_escape_string($_POST['id']);

$delete = "DELETE FROM `xxx`.`xxx` WHERE `xxx`.`ID` = '$id'"; 
$del = mysql_query($delete);

if ($del)
{
    echo "succes";

}
else
{
    echo "error";
    // close connection 
    mysql_close();
}
?>

when I press the delete button, I do get the succes message but the row hasn't been deleted of my database.

当我按下delete按钮时,我确实得到了succes消息,但是我的数据库中没有删除该行。

I have been trying to fix this, but I can't seem to make it work

我一直在试图解决这个问题,但我似乎不能让它奏效

(left out my db connect code)

(漏写我的db connect代码)

2 个解决方案

#1


2  

My Advice to you, is to NEVER delete from your database.

我给你的建议是,永远不要从你的数据库中删除。

Instead, add a boolean variable to your table, which is by default set to '1' and whenever you want to "delete" a row, Set your bool to '0'.

相反,在表中添加一个布尔变量,默认设置为“1”,当您想“删除”一行时,将bool设置为“0”。

When you query your table to show on your page, query WHERE boolVariable = '1'.

当您查询表以显示在页面上时,查询boolVariable = '1'。

For security reasons, this is the best approach, and make sure to remove the "delete" priviledge from the Db user you're using to connect to your database

出于安全原因,这是最好的方法,并确保从要连接到数据库的Db用户中删除“delete”特权


One more thing, stop using mysql_query, and use mysqli_query as mysql_ is deprecated

还有一件事,停止使用mysql_query,并在不赞成使用mysql_时使用mysqli_query

source: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

来源:mysql扩展被弃用,将来会被删除:使用mysqli或PDO代替。

#2


1  

Changing $(this).id to $(this).attr('id') fixed it

改变(美元)。id为$(this).attr('id')修复它

#1


2  

My Advice to you, is to NEVER delete from your database.

我给你的建议是,永远不要从你的数据库中删除。

Instead, add a boolean variable to your table, which is by default set to '1' and whenever you want to "delete" a row, Set your bool to '0'.

相反,在表中添加一个布尔变量,默认设置为“1”,当您想“删除”一行时,将bool设置为“0”。

When you query your table to show on your page, query WHERE boolVariable = '1'.

当您查询表以显示在页面上时,查询boolVariable = '1'。

For security reasons, this is the best approach, and make sure to remove the "delete" priviledge from the Db user you're using to connect to your database

出于安全原因,这是最好的方法,并确保从要连接到数据库的Db用户中删除“delete”特权


One more thing, stop using mysql_query, and use mysqli_query as mysql_ is deprecated

还有一件事,停止使用mysql_query,并在不赞成使用mysql_时使用mysqli_query

source: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

来源:mysql扩展被弃用,将来会被删除:使用mysqli或PDO代替。

#2


1  

Changing $(this).id to $(this).attr('id') fixed it

改变(美元)。id为$(this).attr('id')修复它