I need to know how can I delete the $imagenes form the path in my db...I can't do it successfully(in the db is successfully deleted)...I already put the unlink code but in error log show me the id of the image but not the name of the image...here is my PHP code:
---EDITED---
so I don't know how make it to delete the $imagenes in that id(idToDelete)...can I SELECT the table before or later?? or is not necessary
here is the table
- id_imagen (int PK)
- imagenes (varchar 100)
- id_paciente (int FK)
- f_imagen (current_timestamp)
in personal.php I have the image and with ajax call I try to delete it:
/*the styles of del button and wrapper */
<style type="text/css">
.del_wrapper{float:right;}
.content_wrapper {
max-width: 100%;
margin-right: auto;
margin-left: auto;
}
</style>
/*the ajax call */
<script type="text/javascript">
$(document).ready(function() {
$("body").on("click", "#gallery .del_button", function(e)
{
e.returnValue = false;
var clickedID = this.id.split('-');
var DbNumberID = clickedID[1];
var myData = 'recordToDelete='+ DbNumberID;
jQuery.ajax({
type: "POST",
url: "delimage.php?ts=" + new Date().getTime(),
dataType:"text",
data:myData,
success:function(response){
$('#item_'+DbNumberID).fadeOut("slow");
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
});
});
</script>
/*the image gallery */
<div id="gallery">
<?
$sql = $conn->prepare("select * from IMAGENES where id_paciente = $_GET[id_paciente] order by id_imagen ASC");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
$id_imagen = $row['id_imagen'];
$imagenes = $row['imagenes'];
echo "<div class='del_wrapper' id='item_".$row['id_imagen']."'><a href='#' class='del_button' id='del-".$row['id_imagen']."'>";
echo "<img src='../images/icon_del.gif' border='0' />";
echo "</a>";
echo "<a href='../$imagenes' class='group4'>";
echo "<img src='../$imagenes' class='image_thumbnail' />";
echo "</a> </div>";
}
?></div>
and the code in delimage.php with the select:
<?
include_once("config.php");
if(isset($_POST["recordToDelete"]) && strlen($_POST["recordToDelete"])>0 && is_numeric($_POST["recordToDelete"]))
{
$stmt=$conn->prepare("SELECT id_imagen,imagenes FROM IMAGENES where id_imagen = $_GET[id_imagen]");
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)){
$recordToDelete=$data['imagenes'];
unlink("../imagenes/$imagenes");
}
$idToDelete = filter_var($_POST["recordToDelete"],FILTER_SANITIZE_NUMBER_INT);
if($stmt=$conn->prepare("delete from IMAGENES WHERE id_imagen=$idToDelete"))
$stmt->bindParam("$idToDelete",$id_imagen,PDO::PARAM_INT);
$stmt->execute();
$dbh = null;
}
?>
the ajax call works because in fiddler I see the id of the image that will delete it in delimage.php but only delete the path in db and not the image inside of imagenes folder