2

我有一个名为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。怎么做?我无法思考并找到解决方案。

请帮忙。

4

2 回答 2

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.";
}
于 2012-04-15T02:56:49.540 回答
0

这里的逻辑是,在编辑操作时,所有相关产品都会重新填充到数据库中。如果它们被重新填充,那么最简单的方法是之前将它们删除。

为了实现这一点,在提交时,就在您向related_products表中插入任何内容之前,删除所有products_id等于的行$products_id。在代码的两行之间执行此操作:

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

foreach($insert_rp_ids1 as $id){

此外,您可以考虑使用事务,或者,如果您的表不支持,请检查删除语句是否成功。

于 2012-04-15T10:31:21.397 回答