简体   繁体   English

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

[英]removing row from database through delete button in a php table

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. 该页面显示了表格中的每一行,并在其后面添加了一个删除按钮。

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. 当我按下删除按钮时,我确实收到了成功消息,但该行尚未删除我的数据库。

I have been trying to fix this, but I can't seem to make it work 我一直试图解决这个问题,但我似乎无法使其发挥作用

(left out my db connect code) (省略了我的数据库连接代码)

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'. 当您查询要在页面上显示的表时,查询WHERE 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用户中删除“删除”权限


One more thing, stop using mysql_query, and use mysqli_query as mysql_ is deprecated 还有一件事,停止使用mysql_query,并使用mysqli_query,因为不推荐使用mysql_

source: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead source: 不推荐使用mysql扩展,将来会删除它:使用mysqli或PDO代替

将$(this).id更改为$(this).attr('id')修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM