gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:48:36 26 4
gpt4 key购买 nike

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

---已编辑---

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

这是表格

<小时/>
  • id_imagen(int PK)
  • 图像(varchar 100)
  • id_paciente(int FK)
  • f_imagen(当前时间戳)
<小时/>

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文件夹内的图像

最佳答案

第一:使用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
?>

首先,尝试理解每一行,其次 - 备份数据库,然后 - 仔细尝试示例数据。

关于jquery - 当我使用PDO删除mysql数据库中的路径时如何删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17054263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com