如果复选框被选中,则插入到数据库

问题描述

我只想将那些末尾带有选中复选框的行插入到我的 mysql 数据库中。

由于我有多个具有相同名称的表格和表格数据单元格,因此我的数据存储在数组中(product_name[] 等):

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
                            <?php    
                                while($res = mysqli_fetch_array($result)) { 

                                    echo "<table id='tbl'>
                                    <thead><th name='category[]'>".$res['category_name']."</th></thead>                                   
                                    <tbody>
                                    <tr>
                                        <td><input name='product_name[]' id='name' type='text'></td>
                                        <td><input name='quantity[]' type='text' value='1'></td>
                                        <td><input type='checkbox' name='check[]'></td>
                                    </tr>
                                    </tbody>";
                            
                                }
                            ?>
                            </table>
                        <div class="bottom-btn">
                            <button id="submit" type="submit">Lisa märgitud külmkappi</button>
                        </div>
</form>

我是否也必须将复选框存储在数组中(check[])?

我用于插入的 php 代码:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $product_name = $_POST['product_name'];
        $quantity = $_POST['quantity'];
        // $check = $_POST['check'];
            
foreach($product_name as $i=>$product_name)
            {if(!empty($product_name)){

                    $sql = "INSERT INTO products (id,product_name,category,quantity,expiration_date,barcode) VALUES ('','$product_name','','$quantity[$i]','')";
                        
                    if($stmt = mysqli_prepare($conn,$sql)){
                        mysqli_stmt_bind_param($stmt,"ss",$param_product_name,$param_quantity);
                        
                        $param_product_name = $product_name;
                        $param_quantity=$quantity;

                        
                        if(mysqli_stmt_execute($stmt)){
                            header("location: myFridge.php");
                        } else{
                            echo "Something went wrong. Please try again later.";
                        }
                        mysqli_stmt_close($stmt);
                    }
            }
        }
   
        mysqli_close($conn);
    }

到目前为止,我已经尝试过这个: if(!empty($_POST['check'])) 和这个 if(isset($_POST['check']) ,但那些没有用。

如何在将数据插入数据库之前检查复选框是否被选中?

编辑。工作。

我将此代码添加到我的页面以创建一组复选框的开启和关闭值:

function getAllCheckboxValues($check){
        $found = array();
        foreach ($check as $key => $val){
            if($val == 'on'){
                $found[] = $key;
            }
        }
        foreach($found as $kev_f => $val_f){
            unset($check[$val_f-1]);
        }
        $final_arr = array();

        return $final_arr = array_values($check);

    }

    $checkbox_arr = getAllCheckboxValues($_POST['check']);

并且还在每个复选框前添加了一个隐藏的输入:

<input type='hidden' name='check[]' value='off'>

然后将 checbox 数组与我的 products 数组相匹配,瞧!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)