动态创建多个单选按钮插入sql

时间:2018-07-09 09:56:58

标签: php html5 mysqli

程序员,烦人的家伙再次在这里惹恼您,所以我今天的问题是我有此HTML表单,该表单用于在SQL中插入单选按钮

<?php $q=mysqli_query($dbc,"SELECT * FROM students");
                while ($r=mysqli_fetch_assoc($q)):
        ?>
            <tr>
                <td><?=$r['students_id']?></td>
                <td><?=$r['students_name']?></td>
                <td><?=$r['fname']?></td>
                <td><?=$r['students_reg']?></td>
                <td><?=$r['gender']?></td>
                <td>
                    <form action="" method="POST" role="form" enctype="multipart/form-data">
                        <div class="radio">
                            <label>
                            <input type="radio" name="attendance[<?=$r['students_id']?>]" value="Present" required>
                                    Present     
                            </label>
                            <label for="">
                            <input type="radio" name="attendance[<?=$r['students_id']?>]" value="Absent">
                                    Absent
                            </label>
                        </div>
                    </form>
                </td>
            </tr>
        </tbody>
        <?php endwhile ?>
    </table>    
    <button class="btn btn-success form-control"  name="add_attendance">Register</button>   

the out i get is this

我的问题是我如何插入所有动态创建的选定单选按钮,可以在其中插入与他们的ID平行的值(在db sql中)?

if(isset($_POST['check_list'])){
  $selected = implode(",", $_POST['check_list']);
  $q=mysqli_query($dbc,"UPDATE students SET attendance='$selected'");
  if ($q) {
    # code...
    $msg="Data Has Been Saved";
    $sts="success";
  }
  else{
    #code...
    $msg=mysqli_error($dbc);
    $sts="danger";
  }
}

现在我更新我的代码,当我提交与ID平行的当前和不存在的存储区一次时,我想要的是在所有字段中都存在或不存在

1 个答案:

答案 0 :(得分:0)

attendance是数组输入变量,因此它返回Array

您应该使用for循环来插入/更新值,

例如,

$att= $_POST['attendance'];
$student_id = $_POST['students_id'];
for($i = 0; $i < count($student_id); $i++)
{
  $q=mysqli_query($dbc,"UPDATE students SET attendance='$att[$i]' WHERE students_id=$student_id[$i]");

}