【问题标题】:Multiple radio buttons inside while loopwhile循环内有多个单选按钮
【发布时间】:2017-06-29 20:51:41
【问题描述】:

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

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


?>

【问题讨论】:

  • 你能提供一些示例数据吗?我们显然无法自行测试代码,因为我们无权访问数据库。所以给我们一个示例数据对象会很有帮助。
  • 我有一个表名作为 class1,我在其中存储有关学生的信息,例如 student_id、first_name、last_name、date_of_birth、parents_name、admission_date、admission_fee、first_installment、second_installment。从这个表中,我检索 first_name 和 last_name 并将其存储在名为 class1attendance 的表中,其中我存储学生的 first_name、last_name 和出勤率、卷号。出席将出席和缺席的地方。我希望它对你有所澄清。

标签: php mysql


【解决方案1】:

您的所有单选按钮组都需要唯一的names

每个学生都有一个小组:“缺席”和“出席”。

所以你需要做类似的事情

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 属性。每个学生都有 2 个单选按钮。这两个单选按钮都有这个名字。

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

【讨论】:

  • 嘿,伙计,感谢您的回复,我尝试在单选按钮输入中用 $name+absent 和 $name+present 替换我的 name 变量,但它仍然没有为我锻炼。
  • 从您向我展示的内容来看,您为每个学生创建了太多唯一名称。请看我上面的编辑。
  • 感谢 Tamb 的精彩解释,它使我的概念清晰,但并没有帮助我解决这个问题。再次感谢
猜你喜欢
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2019-07-01
相关资源
最近更新 更多