jeudi 26 octobre 2023

how to display checkbox data and delete sql row after unchecking it? [closed]

I have 2 tables and would like to know how to: 1)display using checkbox by product_id with it checked if there color_id is in the product_attributes table 2)delete the relative row in product_attributes table after I uncheck the box and submit the form

table 1: product_attributes

id product_id color_id
1 2 1
2 2 3

table 2: color_master

id color status
1 Red 1
2 Green 1
3 Blue 1

enter image description here

I tried setting all color check box input with name="color_id[]", also added the hidden input when displaying the colors that are in table 1

<input type="hidden" name="attr_id[]" value='<?php echo $list['id']?>'/>

and then if(isset($_POST['submit'])), I tried

if(isset($_POST['color_id'])){
foreach($_POST['color_id'] as $val){
 $color_id=get_safe_value($con,$_POST['color_id'][$key]);
 $attr_id=get_safe_value($con,$_POST['attr_id'][$key]);
                
if($attr_id>0){
mysqli_query($con,"DELETE from product_attributes WHERE product_id='$id' AND color_id NOT IN('$color_id')");
}else{
mysqli_query($con,"insert into product_attributes(product_id,color_id) values('$id','$color_id')");
}
                
}
} ```



Aucun commentaire:

Enregistrer un commentaire