【问题标题】:insert check box values into database table in php在php中将复选框值插入数据库表
【发布时间】:2021-04-03 16:50:49
【问题描述】:

我目前正在开发一个网络应用程序。我需要将复选框值插入到一个表列名称中作为早餐。当我运行代码数据时插入正常。但它在页面上显示错误消息,未定义索引:早餐和内爆():传递的参数无效。这是我的php代码;

'

$checkBox = implode(",", $_POST['breakfast']);

if(isset($_POST['add'])) {

$query1="INSERT INTO dietplans (breakfast) VALUES ('" . $checkBox . "') ";     

mysqli_query($db,$query1);

}

?>'

这是 HTML 代码;

        <h4><b>Breakfast(8.a.m.)</b></h4>
        <div class="form-check">
         
          <input type="checkbox" value="Full-fat milk  1 cup" name="breakfast[]">
          <label>Full-fat milk  1 cup</label>
        </div>
         <div class="form-check">
          <input type="checkbox" value="2 thin slices of bread with margerine " name="breakfast[]">
          <label>2 thin slices of bread with margerine </label>
        </div>
         <div class="form-check">
        
          <input type="checkbox" value="2 medium size bananas " name="breakfast[]">
          <label>2 medium size bananas</label>
        </div>
         
         
        <button type="submit" name="add" class="btn btn-dark ">Add my breakfast</button

【问题讨论】:

    标签: php


    【解决方案1】:

    这些是您需要定义其索引的数组

    for($i=0; $i<count($_POST['breakfast']); $i++){
       $query1="INSERT INTO dietplans (breakfast) VALUES ('" . 
        $_POST['breakfast'][$i] . "') ";     
        mysqli_query($db,$query1);
      }
    

    这将遍历所有提交的字段,并为从 0 开始的每个索引插入

    【讨论】:

    猜你喜欢
    • 2010-09-19
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多