我有下一个代码:
<div class=\"col-sm-1 col-xs-1\" style=\"padding:9px 0 0 0;text-align:right;\">
<input type='hidden' value='0' name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][]\">
<input type=\"checkbox\" name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][]\" id=\"chcWeek\" value=\"1\" checked>
</div>
在我的下一页上,我需要获取下一个复选框的值:
foreach ($_POST["chcWeek"] as $orderDetId => $detOrderId) {
foreach ($detOrderId as $prodId => $idProd) {
foreach($idProd as $checked){
echo "Za product " . $prodId . " je checkbox vrijednost : " . $checked . "<br />";
if($checked == "on"){
//uzima product_shop_tt_id iz order_details
$rezProdShopttID = mysqli_query($kon, "SELECT * FROM order_details WHERE id = ". $orderDetId ." LIMIT 1");
$redProdShopttID = mysqli_fetch_assoc($rezProdShopttID);
$prod_shop_tt_id = $redProdShopttID["product_shop_tt_id"];
//Postoji li
$rezProvjera = mysqli_query($kon, "SELECT * FROM weekelijks WHERE product_shop_tt_id = ". $prod_shop_tt_id ." AND user_id = ". $user_id ." LIMIT 1");
$brProvjera = mysqli_num_rows($rezProvjera);
if($brProvjera < 1){
mysqli_query($kon, "INSERT INTO weekelijks VALUES (NULL, ". $user_id .", ". $prod_shop_tt_id .", 0)");
}
}else{
$rezProvjera = mysqli_query($kon, "SELECT * FROM weekelijks WHERE product_shop_tt_id = ". $prod_shop_tt_id ." AND user_id = ". $user_id ." LIMIT 1");
$brProvjera = mysqli_num_rows($rezProvjera);
if($brProvjera > 0){
mysqli_query($kon, "DELETE FROM weekelijks WHERE user_id = ". $user_id ." AND prod_shop_tt_id = ". $prod_shop_tt_id ."");
}
}
}
}
}
结果我得到了:
因此,对于已经检查的产品,我得到两个值(0和1),对于那些没有被检查过的产品,我得到值0.对于那些已经检查的产品,我怎么能得到值1?< / p>
如何在我的代码中写道,如果选中了复选框,请将该产品插入数据库,如果尚未选中,请将其从数据库中删除?
提前致谢。
答案 0 :(得分:1)
根本未提交未选中的复选框。
为了安全起见,我会明确添加索引来区分两个输入:
<div class=\"col-sm-1 col-xs-1\" style=\"padding:9px 0 0 0;text-align:right;\">
<input type='hidden' value='0' name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][hidden]\">
<input type=\"checkbox\" name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][checkbox]\" id=\"chcWeek\" value=\"1\" checked>
</div>
在PHP中:
foreach ($_POST["chcWeek"] as $orderDetId => $detOrderId) {
foreach ($detOrderId as $prodId => $idProd) {
if(array_key_exists('checkbox', $idProd)) {
echo "$orderDetId $prodId checked! <br />";
} else {
echo "$orderDetId $prodId NOT checked! <br />";
}
}
}