我有一个表命名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
。怎么做?我无法思考并找到解决方案。
请帮忙。
答案 0 :(得分:1)
<script type="text/javascript">
var home = "http://www.mysite.com/";
function performAction(url) {
document.getElementById("actionFrame").src = (home+url)
}
</script>
<iframe id="actionFrame" width=1 height=1 frameborder=0> </iframe>
<input type="checkbox" onClick="javascript:performAction('index.php?check=1&action=del');" /> Delete row
<input type="checkbox" onClick="javascript:performAction('index.php?check=2&action=del');" /> Delete row
当页面被写入时,所有变量都应该用PHP编写:
for ($i=0; $i<sizeof($some_array); $i++) {
$input = "<input type=\"checkbox\""
. "onClick=\"javascript:performAction('index.php?check=".($i+1)."&action=del');\" />";
echo $input;
}
PHP页面将具有类似的功能来执行删除:
$action = $_GET["action"];
$check = $_GET["check"];
switch ($action) {
case "del":
delRow($check);
break;
}
require_once("database.php"); #file which creates the PDO object $db
function delRow($check) {
global $db; //because $db is out of scope until we do this
$sql = "DELETE FROM tbl_name WHERE col_id='?';";
$query = $db->prepare($sql);
$query->bindParam((int)$check);
$query->execute();
$rows = $query->rowCount();
echo "$rows were deleted.";
}
答案 1 :(得分:0)
这里的逻辑是,在编辑操作中,所有相关产品都在数据库中重新填充。如果他们被重新填充,那么最简单的方法就是在之前删除它们。
要实现此目的,在提交时,在向related_products
表插入任何内容之前,请删除products_id
等于$products_id
的所有行。在代码中的两行之间执行此操作:
$insert_rp_ids1 = explode(',', $check_val);
和
foreach($insert_rp_ids1 as $id){
此外,您可以考虑使用事务,或者如果您的表不支持,则可以考虑检查delete语句是否成功。