2013-06-11 27 views
0

我需要知道如何删除$ imagenes形式的路径在我的数据库...我不能做到这一点成功(在数据库是成功删除)...我已经把取消链接代码,但在错误日志中显示我的图像的ID,但不是图像的名称...这是我的PHP代码:如何删除图像,当我删除与PDO的MySQL数据库的路径

--- EDITED-- -

所以我不知道如何使它在该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调用的作品,因为在提琴手我看到图像的id将删除它在delimage.php,但只有d elete以dB为单位,而不是图像的路径imagenes文件夹内

+0

你不能混合使用$ POST []和$ GET []。你使用什么取决于FORM方法 –

回答

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

首先,尝试understant每一行,第二 - 让你的数据库的备份,之后 - 精心尝试sampple数据。

+0

我已经编辑问题并把表结构..但不能得到ID与选择 –

+0

我将编辑所有的问题。 –

+0

我试过你的代码,但没有工作..不给我错误日志文件中的任何错误..谢谢你的支持! –