虽然循环使用单选按钮将值插入数据库,适用于许多用户


While loop to insert values into database using radio button for many user

此函数用于将值插入数据库。 首先,它会列出班级"$class"中的所有学生。 界面如下:

我不能发布图片,因为我没有太多的名声。但是,界面是它将列出所有带有"否","出生号","学生姓名","出勤"列的学生。在列出勤中,它将显示 3 个单选按钮,即 PT、AT、MC。 表中有一个提交按钮。

问题是,当我在单击两个学生的单选按钮后单击提交按钮时,数据库中没有插入任何内容。

$id = 1;
$getdata = mysql_query("select * from student where class = '$class' order by name ")     or die(mysql_query);
while($row = mysql_fetch_assoc($getdata))
    {
        if(isset($_POST['a'.$id])) 
        {
            $status = $_POST['a'.$id];      
            if(!empty($status))
            {
                if($status == "present")
                {
                    $attend = 1;
                }
                else if($status == "absent")
                {
                    $attend = 0;
                }
                else if($status == "mc")
                {
                    $attend = 1;
                }
                $query = "INSERT INTO attendance VALUES ('$birth_no','$date','$status','$attend')";
                if($query_run = mysql_query($query))
                {
                    echo 'Insert attendance done';
                }
                else
                {
                    echo'Attendance not inserted.';
                }                   
            }
            else
            {
                echo 'Please enter all fields';
            }
        }
        else
        {
            //FORM CODE HERE
            ?>
            <form action="addattend.php" method = "POST">
            <?php
                $birth_no= $row['birth_no'];
                $name = $row['name'];
                    ?>
                    <tr>
                        <td><center><?php echo $id ?></center></td>
                        <td><center><?php echo $date ?></center></td>
                        <td><center><?php echo $birth_no ?></center></td>
                        <td><center><?php echo $name ?></center></td>
                        <?php
                    echo'<td>
                            <input type="radio" name="a'.$id.'" value="present">PT
                            <input type="radio" name="a'.$id.'" value="absent">AT
                            <input type="radio" name="a'.$id.'" value="mc">MC
                        </td>
                    </tr> ';
        }
        $id++;      
    }
            ?>
            </table>
            <center><input type="submit" value="Submit"></center>
            </form>
            <?php

有人可以帮助我解决这个问题吗?我尝试解决这个问题一周。但是什么也没出来。真的很感谢你的好意。谢谢。

我真的不明白 - 您正在循环一个名为 $_POST['a'.$id.''] 的数组,但是如果我查看您的表单,您的输入中没有创建这样的数组。您正在使用像 <input type="radio" name="a'.$id.'" value="mc">MC 这样的单选按钮,如果它处于活动状态,它将创建一个以"mc"作为值的$_POST['a1']。但是$_POST['a1']不是一个数组,它只包含这个值!

所以你应该踢出foreach循环,把mit $_POST['a'.$id]作为你的$status。在其他作品中,制作

$status = $_POST['a'.$id] // by the way you do not need .'' in the end of the key

而不是你的 foreach-loop。

此外,您的脚本似乎不安全。您没有使用 csrf、xss-和 sql 注入保护。你真的需要了解php应用程序(csrf,xss,sql注入)的安全性。这是非常重要的!

看看这是否有帮助。我根据上面的各种评论对表单显示/处理逻辑进行了一些更改。我没有机会对此进行测试,因此这里和那里可能存在小的格式错误。此外,应该可以进一步简化此代码。

$getdata = mysql_query("select * from student where 
         class = '$class' order by name ")     or die(mysql_query);
// form processing logic goes here
if (isset($_POST['submit'])){
    $id = 1;
    while($row = mysql_fetch_assoc($getdata)){
        if(isset($_POST['a'.$id])) {
            $status = $_POST['a'.$id];      
            if(!empty($status)){
                if(($status == "present" || $status == "mc")){
                    $attend = 1;
                }
                else if($status == "absent"){
                    $attend = 0;
                }
                $query = "INSERT INTO attendance(birth_no, date, status, attend) 
                VALUES ('$birth_no','$date','$status','$attend')";
                if($query_run = mysql_query($query)){
                    echo 'Insert attendance done';
                }
                else{
                    echo'Attendance not inserted.';
                }                   
            }
            else{
                echo 'Please enter all fields';
            }
        }
        $id ++;
    } // end while
} // end if
// show the form here
else {
?>   
    <form action="addattend.php" method = "POST">
    <table>
<?php
    $id = 1;
    while($row = mysql_fetch_assoc($getdata)){
        $birth_no= $row['birth_no'];
        $name = $row['name'];
?>
        <tr>
            <td><center><?php echo $id ?></center></td>
            <td><center><?php echo $date ?></center></td>
            <td><center><?php echo $birth_no ?></center></td>
            <td><center><?php echo $name ?></center></td>
            <td>
                <input type="radio" name="a<?php echo $id; ?>" value="present">PT
                <input type="radio" name="a<?php echo $id; ?>" value="absent">AT
                <input type="radio" name="a<?php echo $id; ?>" value="mc">MC
            </td>
        </tr>
<?php
        $id ++;
    } // end while
?>
    </table>
    <center><input type="submit" name="submit" value="Submit"></center>
    </form> <!-- end the form here -->
<?php    
} // end else
?>