使用PDO删除mysql数据库中的路径时如何删除图像

时间:2013-06-11 21:37:32

标签: jquery mysql file-io pdo

我需要知道如何从我的数据库中删除$ imagenes形式的路径...我不能成功(在数据库中成功删除)...我已经把unlink代码但是错误log显示图像的id但不显示图像的名称...这是我的PHP代码:

--- --- EDITED

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

这是表格


  • 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中我看到将在delimage.php中删除它的图像的id但只删除了db中的路径而不是imagenes文件夹中的图像

1 个答案:

答案 0 :(得分:1)

首先:以下列方式使用bindParam

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

但在此之前,您可能必须使用SELECT才能获取文件的名称。之后在unlink中使用该名称,而不是具有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
?>

首先,尝试理解每一行,第二步 - 备份数据库,之后 - 仔细检查采样数据。