我有一个问题,如何从img_ids
删除所需的号码。正如您在下面的表格中看到的img_ids
,如68,67,66,65,64,
。我想删除例如数字67
,我怎么能用PHP做到这一点。由于逗号,我的思绪很混乱:S。
<div class="delete" id="67">Delete Image</div>
的Ajax
$("body").on("click", ".delete", function(){
var deleteimgid = $(this).attr("id");
var data = 'img='+deleteimgid;
$.ajax({
type: 'POST',
url: 'delete.php',
data: data,
cache: false,
beforeSend: function(){},
success: function(response){
}
});
});
delete.php
<?php
include_once 'inc.php';
if(isset($_POST['img'])){
$imgid = mysqli_real_escape_string($db, $imgid);
// Then what should be the query ?
// I am confused because of the commas.
}
?>
答案 0 :(得分:4)
即使你不应该存储这样的数据,这是一种有效的方法:
$imgList = '12,23,45,56,67,78';
$imgid = 12;
$imgIds = explode(',', $imgList);
$imgIds = array_filter($imgIds, function ($item) use ($imgid) {
return $imgid != $item;
});
$imgList = implode(',', $imgIds);
print $ImgList; // prints 23,45,56,67,78
它的作用:
您实际应该/可以存储数据的方式
正如其他人在评论中指出的那样,您应该规范化您的数据库。 Read more
您还可以使用blob字段而不是文本字段,因为PHP可以使用serialize / unserialize
轻松处理blob数据