Php 多个单选按钮在while循环中

Php 多个单选按钮在while循环中,php,mysql,Php,Mysql,大家好,我正试图在这个程序中构建一个简单的课堂出勤系统。首先,我从名为class1的表中获取名称值,然后,我尝试将该名称和出勤插入名为class1的表中。但当我试图提交它时,无论我选择第一个单选按钮什么,所有学生都会得到相同的值。例如,我有很多学生。第一个学生是Richard。当我为Richard选择present单选按钮时,我会为其他学生获得存储在数据库中的礼物。这是我的密码 <?php include('header.php'); include('mysqli_connect.p

大家好,我正试图在这个程序中构建一个简单的课堂出勤系统。首先,我从名为class1的表中获取名称值,然后,我尝试将该名称和出勤插入名为class1的表中。但当我试图提交它时,无论我选择第一个单选按钮什么,所有学生都会得到相同的值。例如,我有很多学生。第一个学生是Richard。当我为Richard选择present单选按钮时,我会为其他学生获得存储在数据库中的礼物。这是我的密码

<?php 

include('header.php');
include('mysqli_connect.php');
echo "<br> <br>";

$q = "SELECT CONCAT(first_name, ' ', last_name) AS Name FROM class1";

$r = mysqli_query($dbc, $q);

echo '<div class = "container">';

echo '

<table>

<tr class = " w3-padding w3-green w3-xlarge "> <td> Student Name </td>

<td> Absent/Present </td>

 </tr>';

 $attendance ; 

 if($_SERVER['REQUEST_METHOD'] == 'POST')
{
     $attendance ; 
    if($_POST['attendance'] == 'present')
    {
        $attendance = 'present';
    }
    else
    {
        $attendance = 'absent';
    }

}

while($row = mysqli_fetch_array($r))
{
    $name =  $row['Name'];
    $q1 = "INSERT INTO class1attendance(s_id, first_name, attendance) VALUES (0, '$name', '$attendance')";

    $r1 = mysqli_query($dbc, $q1);
    echo '<tr> 
    <td>' . $name . '</td> <td>
    <form method = "post" action = "">
    Present <input type = "radio" name = "attendance" value = "present" class = "w3-radio">
    Absent <input type = "radio" name = "attendance" value = "absent" class = "w3-radio">
    </td>
    <td>
    <input type = "submit" name = "submit" value = "Submit Attendence" class = "w3-btn w3-green w3-round">
    </td>
    </tr>
    </form>';;


}


echo '</div>
</table>';


?>

所有单选按钮组都需要唯一的
名称

每个学生得到一组:“缺席”和“出席”

所以你需要做一些类似的事情

name=" $first_name + $last_name +'attendance'"  
Idk php,但这就是想法。否则,您只生成一个广播组,因此获得一个值

____编辑____

最终,您希望您的HTML如下所示:

<table>
    <!-- radio group 1 -->
    <tr>
      <td>
        John Doe
      </td>
      <td>
        <label for="JohnDoe0">
          Absent
          <input id="JohnDoe0" type="radio" name="JohnDoe_attendance" value="absent"/>
        </label>
      </td>
      <td>
        <label for="JohnDoe1">
          Present
          <input id="JohnDoe1" type="radio" name="JohnDoe_attendance" value="present"/>
        </label>
      </td>
    </tr>
    <!--/ end radoi group 1 -->

    <!-- radio group 2 -->
    <tr>
      <td>
        Jane Doe
      </td>
      <td>
        <label for="JaneDoe0">
          Absent
          <input id="JaneDoe0" type="radio" name="JaneDoe_attendance" value="absent"/>
        </label>
      </td>
      <td>
        <label for="JaneDoe1">
          Present
          <input id="JaneDoe1" type="radio" name="JaneDoe_attendance" value="present"/>
        </label>
      </td>
    </tr>
    <!--/ end radoi group 2 -->
  </table>

无名氏
缺席的
目前
无名氏
缺席的
目前
因此,为了正确地接收您的值,每个无线电组的每个名称都必须是唯一的

因此,如果你有20个学生,你就有20个独特的
name
属性。每个学生有两个单选按钮。这两个单选按钮都有这个名字


注意:我不会把表格放在桌子上。我会使用CSS来格式化表单。但是,我会在表格中显示该表单的结果。

嘿,伙计,谢谢你的回复,我试图用单选按钮输入中的$name+缺席和$name+存在替换我的name变量,但它仍然不适合我。根据你给我的显示,你为每个学生创建了太多的唯一名称。请看我上面的编辑。谢谢Tamb的精彩解释。它让我的概念清晰了,但并没有帮助我解决这个问题。谢谢你提供了一些样本数据?显然,我们无法单独测试代码,因为我们无法访问数据库。所以给我们一个示例数据对象会很有帮助。我有一个表名为class1,我在其中存储关于学生的信息,比如学生id、名字、姓氏、出生日期、父母姓名、入学日期、入学费、第一期、第二期。从这个表中,我检索名字和姓氏,并将其存储在名为ClassAttention的表中,其中存储学生的名字、姓氏和出勤、卷号。只有在场和缺席的人才会出席。我希望这能让你明白一点。