【问题标题】:How to delete an image when I delete the path in mysql db with PDO当我使用 PDO 删除 mysql db 中的路径时如何删除图像
【发布时间】:2013-06-07 21:39:09
【问题描述】:

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

---已编辑---

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

这是表格


  • id_imagen (int PK)
  • imagenes (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 调用有效,因为在 fiddler 中我看到图像的 id 将在 delimage.php 中删除它,但只删除 db 中的路径而不是 imagenes 文件夹内的图像

【问题讨论】:

  • $POST[] 和 $GET[] 不能混用,具体用什么取决于 FORM 方法

标签: jquery mysql file-io pdo


【解决方案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
?>

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

【讨论】:

  • 我已经编辑了问题并放置了表格结构..但无法通过 select 获取 id
  • 我尝试了你的代码,但没有工作..在错误日志文件中没有给我任何错误..感谢您的支持!
  • 嗨,我的朋友,我只是稍微改变一下你的代码,效果很好!!感谢您的支持最好的问候我只是在选择中将 :idToDelete 更改为 $idToDelete,然后在删除中,这就是魔法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多