0

我需要知道如何从我的数据库中的路径中删除 $imagenes...我无法成功(在数据库中已成功删除)...我已经放置了取消链接代码但在错误日志中显示图片的 id 但不是图片的名称...这是我的 PHP 代码:

---已编辑---

所以我不知道如何删除该id(idToDelete)中的$ imagenes ...我可以在之前或之后选择表吗?或者没有必要

这是桌子


  • id_imagen (int PK)
  • 图像(varchar 100)
  • id_paciente (int FK)
  • f_imagen (current_timestamp)

personal.php中我有图像并通过ajax调用尝试删除它:

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

delimage.php中的代码与选择:

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

ajax 调用有效,因为在提琴手中,我看到图像的 id 将在 delimage.php 中删除它,但只删除 db 中的路径,而不是 imagenes 文件夹内的图像

4

1 回答 1

1

第一:按以下方式使用bindParam :

$sth = $conn->prepare("DELETE FROM `IMAGENES` WHERE `id_imagen` = :idToDelete")
$sth->bindParam(':idToDelete', $id_imagen, PDO::PARAM_INT);

但在此之前,您很可能必须使用 SELECT 才能获取文件名。之后在取消链接中使用该名称,而不是具有 ID 的变量。如果您需要好的建议,请在此处发布您的表格结构。

很不知道什么是 $_POST["recordToDelete"] 以及为什么你在那之后尝试使用 $_GET 。如果imagenes要删除列存储文件名,根据id_imagen尝试以下方式:

<?
include_once("config.php"); 
/*hope above is the connection with MySQL and that connection is $conn */

if(isset($_POST["recordToDelete"]) && strlen($_POST["recordToDelete"])>0 && is_numeric($_POST["recordToDelete"])) {
  $idToDelete = filter_var($_POST["recordToDelete"], FILTER_SANITIZE_NUMBER_INT);
  /* following will give you file name on the corresponding id from table */
  $stmt = $conn->prepare("SELECT `imagenes` FROM IMAGENES where `id_imagen` = :id_imagen"); 
  $stmt->bindParam(':id_imagen', $id_imagen, PDO::PARAM_INT);
  $stmt->execute();
  if ($result = $stmt->fetch()) {
    /* this will delete the file */
    unlink("../imagenes/" . $result[0]);
    /* and here you will delete the record in DB if this is your intention also */
    if($stmt = $conn->prepare("DELETE FROM IMAGENES WHERE id_imagen = :idToDelete"))
    $stmt->bindParam(":idToDelete", $id_imagen, PDO::PARAM_INT);
    $stmt->execute();
  }
}
$conn = null;    //Disconnect
?>

首先,尝试了解每一行,其次 - 备份您的数据库,然后 - 仔细尝试样本数据。

于 2013-06-11T22:22:12.780 回答