mvasco mvasco - 5 months ago 8
SQL Question

Record not deleted from database

I am using following code to show MySQL records on a web site table.
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:

/* 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

<?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

Answer

You are checking for $_POST['id_peticion'] while the delete id is gonna be in $_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));  
}