首页 > 解决方案 > 我从 mysql 获取数据并勾选它并将其发送回 mysql 记录只保存一个 id 为 3 的学生

问题描述

我从mysql获取数据并将其显示在表中现在我试图通过选中复选框将其发送到每个学生的数据库,那么我应该怎么做才能将所有表数据发送到具有不同学生ID的msql数据库中

<form class="col-md-12" action="Attendance.php" method="post">
    <table id="example" class="myclass table table-striped"  />
        <thead>
            <tr>
                <th>ID</th>
                <th>Full Name</th>
                <th>Father Name</th>
                <th><label><input type="checkbox" id="selectAll" name="chbox[]"> All Present </label></th>
            </tr>
        </thead>
        <tbody>
<?php 
$SrNo = 0;
global $dbManager;

$sql = "SELECT * FROM studentinfo";
$stmt = $dbManager->query($sql);

while($DataRows = $stmt->fetch()){
    $RollNo      = $DataRows['id'];
    $FullName  = $DataRows['fullname'];
    $FatherName  = $DataRows['fathername'];
?>
            <tr>
                <td><?php echo $RollNo; ?></td>
                <td><?php echo $FullName; ?></td>
                <td><?php echo $FatherName; ?></td>
                <td><input type="checkbox" name="chbox[]" value="1" /></td>
            </tr>
        </tbody>
    
<?php } ?>
        <tfoot>
<?php
  
if(isset($_POST['Submit'])){
    $StudentRollNo = $RollNo;
    $Attendance = $_POST['chbox'];

    // date_default_timezone_set("Asia/Kabul");
    $CurrentTime = time();
    $DateTime = strftime("%B-%d-%Y %H:%M:%S", $CurrentTime);
   
    if(empty($Attendance)){
        $_SESSION['ErrorMessage'] = "Please filled all fields";
        RedirectTo("Attendance.php");
    }
    else{
        // the sql code is here.
        global $dbManager;
        foreach ($Attendance as $key => $value) {
        $sql = "INSERT INTO attendance(sid, subjectid, classid, attendance, datetime) VALUES(:studentId,'1','2', :attendancE, :dateTime)";
        $stmt = $dbManager->prepare($sql);
        $stmt->bindValue(':studentId',$StudentRollNo);
        $stmt->bindValue(':attendancE',$value);
        $stmt->bindValue(':dateTime',$DateTime);

        $Execute = $stmt->execute();

        if($Execute){
            $_SESSION['SuccessMessage'] = "Attendance Submited Successfully.";
            RedirectTo("Attendance.php");
        }
        else{
            $_SESSION['ErrorMessage'] = "Something went wrong. Try Again!";
            RedirectTo("Attendance.php");
        }
    }
      
}
?>
            <tr>
                <td>
                <i class="fa fa-check"></i>
                <input type="submit" name="Submit" value="Save Attendance">
                </td>
            </tr>
        </tfoot>
    </table>
</form>

Mysql 数据库:问题是记录只保存一个学生。

mysql数据库的问题是记录只保存一个学生

标签: phphtmlmysql

解决方案


将表带入 while 循环中,从数据库中获取值,然后为复选框动态分配学生的值value

你可以做

value="<?php echo $RollNo; ?>";

就像您回显其他值一样,将回显添加到复选框的值字段中,以便动态分配给每个复选框的值


推荐阅读