简体   繁体   English

记录未从数据库中删除

[英]Record not deleted from database

I am using following code to show MySQL records on a web site table. 我正在使用以下代码在网站表上显示MySQL记录。 This is the column whith the delete button: 这是带有删除按钮的列:

<td align="center"><a id="<?php echo $row['id_peticion']; ?>" class="delete-link" href="#" title="Delete">
            <img src="delete.png" width="20px" />
            </a></td>

This is the JS code called when user clicks on the delete button: 这是用户单击删除按钮时调用的JS代码:

/* Data Delete Starts Here */
    $(".delete-link").click(function()
    {
        var id = $(this).attr("id");
        var del_id = id;
        var parent = $(this).parent("td").parent("tr");
        if(confirm('Seguro que quieres borrar la petición # = ' +del_id))
        {
            $.post('delete.php', {'del_id':del_id}, function(data)
            {
                parent.fadeOut('slow');
            }); 
        }
        return false;
    });
    /* Data Delete Ends Here */

And this is the code from delete.php 这是来自delete.php的代码

<?php
include_once 'dbconfig.php';

if($_POST['id_peticion'])
{
    $id = $_POST['id_peticion'];    
    $stmt=$db_con->prepare("DELETE FROM tbpeticiones WHERE id_peticion=:id");
    $stmt->execute(array(':id'=>$id));  
}
?>

When clicked, the record dissapears from the table but is not deleted from the database. 单击该记录后,该记录将从表中消失,但不会从数据库中删除。

I cannot find the reason why... Thank you 我找不到原因...谢谢

您的帖子键是jquery中指定的del_id而不是id_peticion

$.post('delete.php', {'del_id':del_id}, function(data)

You are checking for $_POST['id_peticion'] while the delete id is gonna be in $_POST['del_id'] . 您正在检查$_POST['id_peticion']而删除ID则位于$_POST['del_id'] Change your code to : 将您的代码更改为:

if($_POST['del_id'])
{
    $id = $_POST['del_id'];    
    $stmt=$db_con->prepare("DELETE FROM tbpeticiones WHERE id_peticion=:id");
    $stmt->execute(array(':id'=>$id));  
}

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

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