我有一个表命名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
上),点击保存按钮后,related_products_ids
3
的值将被删除,以便现在2 and 4
当前行。怎么做?我无法为此思考并找到解决方案。
请帮忙。
<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.";
}
这里的逻辑是,在编辑操作时,所有相关产品都会重新填充到数据库中。如果重新填充它们,那么最简单的方法是在之前删除它们。
为此,在提交时,在向表中插入任何内容之前related_products
删除products_id
等于 $products_id
的所有行。在代码中的两行之间执行此操作:
$insert_rp_ids1 = explode(',', $check_val);
和
foreach($insert_rp_ids1 as $id){
此外,您可以考虑使用事务,或者,如果您的表不支持,则检查 delete 语句是否成功。