【问题标题】:If checkbox is checked then insert to database如果选中复选框,则插入数据库
【发布时间】:2021-06-15 06:10:11
【问题描述】:

我只想在我的 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[])?

我的插入代码:

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'>

然后将复选框数组与我的产品数组匹配,瞧!

【问题讨论】:

  • 复选框的行为与其他表单元素不同,因为它们仅在选中时才会导致表单提交数据集中的条目。解决此问题的另一种方法是,您在表单字段名称中预先指定数组索引name="foo[0]"name="foo[1]" 等。然后您可以循环$_POST['product_name'],并通过数组索引$i,可以检查对应的条目$_POST['check'][$i]是否存在。

标签: php mysql checkbox checked


【解决方案1】:

您只需var_dump($_POST) 即可查看您的帖子请求中的内容。面对此类问题时,这总是有帮助的。

复选框带有 on 值如果它们被选中,off 如果它们没有被选中,所以它永远不会为空并且总是被设置,这就是你的检查不起作用的原因。你应该检查if($_POST['check'] === 'on')

【讨论】:

  • 我尝试从复选框“on”和“off”值创建一个数组,以便我可以将正确的复选框与我的表格行连接起来。但它只向我显示“on”值,因此我的数组不正确。它跳过了“关闭”复选框,因此我的数组中的索引是错误的,它会在我的数据库中插入错误的行。就像如果我的索引为 1 的数组项应该是“关闭”,那么它会跳过它并找到下一个“打开”并将该值放在该数组的索引 1 中,因此稍后它将错误的复选框与错误的表行连接起来。如果这有任何意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2019-05-22
  • 2014-09-04
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多