【问题标题】:Delete row when checkbox previously selected is deselected取消选中先前选中的复选框时删除行
【发布时间】:2012-04-15 02:34:05
【问题描述】:

我有一个表命名为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_ids2, 3 and 4..我取消选中值为3的复选框(位于related_products_ids上)在点击保存按钮后,related_products_ids 上的值3 将被删除,因此当前行现在是2 and 4。怎么做?我无法思考并找到解决方案。

请帮忙。

【问题讨论】:

    标签: php html mysql forms checkbox


    【解决方案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.";
    }
    

    【讨论】:

    • 那不是我想要的..我只是想删除 mysql 表上取消选中的复选框的值..
    • @Ken 这不是很明显,因为那时您在问题中需要的只是 SQL 代码,而不是 HTML 内容。无论如何,我为您提供了一个示例。
    • 感谢您的建议。非常感谢。
    【解决方案2】:

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

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

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

    foreach($insert_rp_ids1 as $id){
    

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

    【讨论】:

    • @Ken 您能否确认当前您的脚本能够向related_products_ids添加 新条目?您是想要删除取消选择的条目还是通过编辑要添加和删除的帖子?
    • 对不起。我找到了问题..它在我的 sql 删除语句中..现在它可以工作了..谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多