0

我有一个名为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下的特定 id 。related_products_ids

假设我正在编辑 id 所在的帖子2以及related_products_ids它的所在位置1,3.. 我取消选中了值所在的复选框3(在 上related_products_ids),在点击保存按钮后,将取消设置3上的值,因此当前值related_products_ids现在是1。怎么做?我无法思考并找到解决方案。

请帮忙。

4

1 回答 1

1

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

这是我们无法轻松操作数据的非规范化数据库的主要困难。

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

于 2012-04-14T03:30:00.323 回答