2013-06-11 56 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

我試過你的代碼,但沒有工作..不給我錯誤日誌文件中的任何錯誤..謝謝你的支持! –