在mysql表上的数组中取消设置特定值

时间:2012-04-14 02:29:32

标签: php mysql unset

我有一个表命名related_products,

其中 products_id是主要产品。

related_products_ids由与主要产品相关的产品ID组成。

--------------------------------------------
|   products_id   |   related_products_ids |
| -----------------------------------------
|    1            |   2,3                  |
| -----------------------------------------
|    2            |   1,3                  |
| -----------------------------------------
|    3            |   1,2                  |
-------------------------------------------

我有复选框,

<input value="1" type="checkbox" name="rp_product[]" id="in-category1"> Microsoft IntelliMouse Pro 1
<input value="2" type="checkbox" name="rp_product[]" id="in-category2"> Microsoft IntelliMouse Pro 2
<input value="3" type="checkbox" name="rp_product[]" id="in-category3"> Microsoft IntelliMouse Pro 3

复选框由php生成,

    <?php
    echo '<div class="categorydiv"><div id="category-all" class="tabs-panel"><ul id="categorychecklist" class="list:category categorychecklist form-no-clear">';
      $rp_sql = "select products_id, products_name from ".TABLE_PRODUCTS_DESCRIPTION." order by products_id";
      $rp_1 = mysql_query($rp_sql);
            while($rp_2 = mysql_fetch_array($rp_1)) {
            echo "<li id=\"category-".$rp_2['products_id']."\" class=\"popular-category\"><label class=\"selectit\"><input value=\"".$rp_2['products_id']."\" type=\"checkbox\" name=\"rp_product[]\" id=\"in-category-1\"> ".$rp_2['products_name']."</label></li>";
            }
    mysql_free_result($rp_1);
    echo '</ul></div></div>';
    ?>

我使用此代码将选中的复选框保存到related_products表,

for ($i=0; $i<count($_POST['rp_product']); $i++)
 {
  $check_val1 .= $_POST['rp_product'][$i] .","; //gather data
 }
$check_val = trim($check_val1, ',');  //clean last ,
unset($check_val1); //flush

$related_products = array('products_id' => (int)$products_id,
                        'related_products_ids' => $check_val);

$insert_rp_ids1 = explode(',', $check_val);

    foreach($insert_rp_ids1 as $rp_ids)
    {
    $result_rp = mysql_query("SELECT related_products_ids, products_id FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . $rp_ids ."'");

     if(mysql_num_rows($result_rp))
     {
       $related_products_value = mysql_fetch_assoc($result_rp);
       $check_dup = explode(',', $related_products_value['related_products_ids']);
        $rp_values = $related_products_value['related_products_ids'] . "," . $products_id;

        $rps_each1 = array('related_products_ids' => $rp_values);
        if(!in_array($products_id, $check_dup)) {
        $wpdb->update( TABLE_RELATED_PRODUCTS, $rps_each1, array( 'products_id' => $rp_ids ));
        }
     }
       else
            {
               $rps_each2 = array('products_id' => $rp_ids, 'related_products_ids' => $products_id);
               $wpdb->insert(TABLE_RELATED_PRODUCTS, $rps_each2);
            }
    }

当取消选中之前选中的复选框时,我想要的是unset related_products_ids列下的特定ID。

让我们说我正在编辑ID为2的帖子以及related_products_ids 1,3的帖子。我取消选中值为3的复选框(在related_products_ids上,在点击保存按钮后,3上的值related_products_ids将被取消设置,因此当前值为1。怎么做?我无法思考并找到解决方案。

请帮忙。

1 个答案:

答案 0 :(得分:1)

您需要规范化您的数据库。在数据库中存储逗号分隔值绝不是一个好主意。

这是这种非标准化数据库的主要困难,我们无法轻易操纵数据。

您应该制作父子表来存储相关产品,而不是像这样存储它们。