mvasco mvasco - 1 year ago 63
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" />

This is the JS code called when user clicks on the delete button:

/* Data Delete Starts Here */
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)
return false;
/* Data Delete Ends Here */

And this is the code from delete.php

include_once 'dbconfig.php';

$id = $_POST['id_peticion'];
$stmt=$db_con->prepare("DELETE FROM tbpeticiones WHERE id_peticion=: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 Source

You are checking for $_POST['id_peticion'] while the delete id is gonna be in $_POST['del_id']. Change your code to :

    $id = $_POST['del_id'];    
    $stmt=$db_con->prepare("DELETE FROM tbpeticiones WHERE id_peticion=:id");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download