我有一个名为related_products 的表,
products_id
主要产品在哪里
。
并由related_products_ids
与主要产品相关的产品 ID 组成。
--------------------------------------------
| products_id | related_products_ids |
| -----------------------------------------
| 1 | 2 |
| -----------------------------------------
| 1 | 4 |
| -----------------------------------------
| 1 | 3 |
-------------------------------------------
我有复选框,
<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
<input value="4" type="checkbox" name="rp_product[]" id="in-category3"> Microsoft IntelliMouse Pro 4
复选框由php生成,
<?php
echo '<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">';
global $wpdb;
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$HTTP_GET_VARS['pID']."'";
$result = mysql_query($sql);
$rps = array();
while($row = mysql_fetch_assoc($result)) {
$rps[]=$row["related_products_ids"];
}
$rp_sql = "select products_id, products_name from ".TABLE_PRODUCTS_DESCRIPTION." where products_id !='" . (int)$HTTP_GET_VARS['pID']."' order by products_name";
$rp_1 = mysql_query($rp_sql);
while($rp_2 = mysql_fetch_array($rp_1)) {
$checked = '';
if (in_array($rp_2['products_id'], $rps)) $checked = " checked";
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[]\" class=\"rp_item\"" . $checked . "> <span>".$rp_2['products_name']."</span></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
$insert_rp_ids1 = explode(',', $check_val);
foreach($insert_rp_ids1 as $id){
$rps_each2 = array('products_id' => $products_id, 'related_products_ids' => $id);
$wpdb->insert(TABLE_RELATED_PRODUCTS, $rps_each2);
}
我想要的是在related_products_ids
取消选择先前选择的复选框时删除列下的特定ID。
假设我正在编辑 products_id 所在的帖子1
以及related_products_ids
它的所在位置2, 3 and 4
.. 我取消选中了值所在的复选框3
(在 上related_products_ids
),在点击保存按钮后,将删除3
上的值,related_products_ids
以便当前行现在是2 and 4
。怎么做?我无法思考并找到解决方案。
请帮忙。