如何解决上载多个图像删除PHP MySqli中多个图像上的选定图像
大家好,我有一个小问题,我将上传多个图像,一个输入文件和一个逗号存储的数据库存储,但是我有问题,就是删除选择的上传图像,然后单击“删除”图标上方的图像删除该图像。图片已删除
<div class="panel-body">
<div class="table-responsive">
<table id="dataTableExample1" class="table table-striped table-bordered">
<thead>
<tr>
<th>S.No</th>
<th>gallery Images</th>
<th>gallery Name</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<?PHP
extract($_REQUEST);
$sql="SELECT * FROM `smw_gallery`";
$result = $conn->query($sql);
$count=$result->num_rows;
if ($count > 0) {
$i=1;
while ($row = $result->fetch_object()) {
$primages = $row->smw_gallery_images;
$imgp = explode(",",$primages);
$realPath = '../assets/images/gallery/';
?>
<tr>
<td style="width:10%"><?=$i;?></td>
<td style="width:50%">
<?PHP foreach($imgp as $img)
{
echo '<a class="example-image-link" href="#" data-lightBox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="'.$realPath.'/'.$img.'" alt="" height="80" width="80" style="margin: 10px;"/></a>';
} ?>
</td>
<td style="width:10%">
<?PHP
$limit = 30;
$td_title1 = $row->smw_gallery_name;
if (strlen($td_title1) > $limit)
$td_title1 = substr($td_title1,strrpos(substr($td_title1,$limit),' '))."...";
echo $td_title1;
?></td>
<td style="width:20%">
<center>
<a href="gallery-edit.PHP?edit=<?=$row->smw_gallery_id;?>" title="Edit"><i class="fa fa-pencil-square-o btn btn-warning" aria-hidden="true"></i></a>
</center>
</td>
</tr>
<?PHP $i++; } } ?>
</tbody>
</table>
</div>
</div>
上面的代码输出看起来像这样 我将放置删除按钮和孔画廊删除按钮的每个图像
解决方法
您可以借助jquery,ajax和mysql的帮助。
在单击删除图标时,您必须使用图像名称和id参数发出一个ajax请求。使用以下查询更新数据库中的图像名称。成功执行ajax响应后,您可以使用jquery删除该图像块。
图片的HTML代码。只是示例行。
<a href="#" data-name="image-name1" class="delete-image">DeleteIcon</a>
jQuery代码
$(document).on('click','.delete-image',function(){
var $this = $(this);
var imagname = $(this).data('name');
$.post("delete_image.php",name: imagname,id: 1
},function(data,status){
$this.closest(tr).remove(); //Write your remove code for single image
});
})
我写的代码看起来像这样:
<img class="btn-delete" id="photo-<?=$photo_index_key;?>" data-id="<?=$photo_index_key;?>" data-name="<?=$photo_name;?>" style="margin: 3px 1px 74px -17px; cursor: pointer;" src="../assets/images/closes.png";>
可以在jquery中获取每个索引键值var $ imageId = $(this).attr('id');
<script>
$(document).on('click','.btn-delete',function(){
var imageId = $(this).attr('data-id');
var imageName = $(this).attr('data-name');
var dataString = {id:imageId,name: imageName}
$.ajax({
type: "POST",url: "remove.php",data: dataString,cache: false,success: function(html){
$('#photo'+imageId).remove(); // you can write your logic
}
});
});
</script>
remove.php
//get ajax data:
$id = $POST['id'];
$name = $POST['name'];
UPDATE smw_gallery
SET `smw_gallery_images` = REPLACE(`smw_gallery_images`,$name,'')
WHERE `id` = $id;
,
如果您在外观上这样做会怎样。 也就是说,您可以按索引定义数组
<div class="panel-body">
<div class="table-responsive">
<table id="dataTableExample1" class="table table-striped table-bordered">
<thead>
<tr>
<th>S.No</th>
<th>Gallery Images</th>
<th>Gallery Name</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<?php
extract($_REQUEST);
$sql = "SELECT * FROM `smw_gallery`";
$result = $conn->query($sql);
$count = $result->num_rows;
if ($count > 0) {
$i = 1;
while ($row = $result->fetch_object()) {
$primages = $row->smw_gallery_images;
$imgp = explode(",",$primages);
$realPath = '../assets/images/gallery/';
?>
<tr>
<td style="width:10%"><?= $i; ?></td>
<td style="width:50%">
<?php foreach ($imgp as $photo_index_key => $img) {
echo '<a class="example-image-link" href="#" data-lightbox="example-set" data-title="Click the right half of the image to move forward.">
<img class="example-image" src="' . $realPath . '/' . $img . '" alt="" height="80" width="80" style="margin: 10px;"/>
</a>';
echo "<a href='".$realPath . "/remove.php?smw_gallery_id=" . $row->id . "&photo_index_key=" . $photo_index_key . "'></a>";
} ?>
</td>
<td style="width:10%">
<?php
$limit = 30;
$td_title1 = $row->smw_gallery_name;
if (strlen($td_title1) > $limit)
$td_title1 = substr($td_title1,strrpos(substr($td_title1,$limit),' ')) . "...";
echo $td_title1;
?></td>
<td style="width:20%">
<div style="text-align: center;">
<a href="gallery-edit.php?edit=<?= $row->smw_gallery_id; ?>" title="Edit"><i
class="fa fa-pencil-square-o btn btn-warning" aria-hidden="true"></i></a>
</div>
</td>
</tr>
<?php $i++;
}
} ?>
</tbody>
</table>
</div>
我不会英语。这是通过Google翻译完成的
remove.php
<?php
if (!empty($_GET['smw_gallery_id'] && !empty($_GET['photo_index_key']))) {
$sql = sprintf("SELECT `smw_gallery_images` FROM `smw_gallery` WHERE `id` = %d",$_GET['smw_gallery_id']);
$result = $conn->query($sql);
$result->fetch_assoc();
if (!is_null($result) && is_array($result)) {
while ($row = $result->fetch_assoc()) {
$smw_gallery_images = explode(",$row['smw_gallery_images']);
$new_smw_gallery_images = array_splice($smw_gallery_images,$_GET['photo_index_key'],1);
$new_smw_gallery_images = implode(',',$new_smw_gallery_images);
$updateSql = sprintf("UPDATE smw_gallery SET `smw_gallery_images` = %s WHERE `id` = %d",$new_smw_gallery_images,$_GET['smw_gallery_id']);
$conn->query($updateSql);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。