2017-10-13 84 views
0

我有一个关于如何从img_ids中删除所需号码的问题。正如您在img_ids下面的表格中看到的,如68,67,66,65,64,。我想删除例如67,我怎样才能用PHP做到这一点。由于逗号,我的头脑混乱:S。从数据中删除所需号码

enter image description here

<div class="delete" id="67">Delete Image</div> 

阿贾克斯

$("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. 
    } 
?> 
+4

处理BLOB数据很容易想想你的规范化数据库,而不是存储逗号分隔的数据...真的很难处理 - 您将遇到与可维护性,可扩展性和性能相关的更大问题 –

+0

**不要**将多个值存储在单个字段中。使用另一个表。 – axiac

回答

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 

做些什么:

  1. 分裂ID列表到一个数组由逗号
  2. 使用array_filter映射阵列并卸下所需的值
  3. 用逗号
  4. 再次内爆列表

您究竟应该/可以存储您的数据

正如其他人在评论中指出的,你应该规范你的数据库。 Read more

你也可以使用一个BLOB字段,而不是文本字段,因为PHP可以使用serialize/unserialize

+0

我真的明白你的答案。我会给你投票。它解决了这个问题。所以我可以问一个来自stackoverflow聊天部分的迷你问题吗? – Azzo

+0

当然,创建一个房间并向我发送链接 –