2017-06-04 216 views
1

我在每个表格行中都有一个复选框,并且我想按下“保存”,它将循环显示每个复选框: (如果选中) - >输入到此学生已参与的数据库 否则 - >进入该学生尚未参加的数据库。循环播放PHP中的复选框

enter image description here

我的PHP代码:

<table border="1" cellpadding="1" width="550px" style="text-align:center;"> 
    <th> Student ID </th> 
    <th> First Name </th> 
    <th> Last Name </th> 
    <th> Class ID </th> 
    <th> Attended </th> 
<?php 
    $classid = $_GET['classid']; 

    $mysql_host='localhost'; 
    $mysql_user='root'; 
    $mysql_password=''; 
    $con = @mysqli_connect($mysql_host,$mysql_user,$mysql_password); 

    if(!$con){ 
     die('Failed to connect to the database');//if not successful 
    }else{ 
     //echo "Successfully connected to MySQL!";//if successful 
     if(@mysqli_select_db($con, 'application_database')){//selecting the database 
      //echo '<br>'."Connected to the specified database!"; 
     }else{ 
      die('<br>'."Could not connect to the specified database!"); 
     } 
    } 

    $sql="SELECT * FROM `student` WHERE class = '$classid'"; 
    $records=mysqli_query($con,$sql); 

    while($student=mysqli_fetch_assoc($records)){ 

     echo "<tr>"; 
     echo "<td>".$student['id']."</td>"; 
     echo "<td>".$student['first_name']."</td>"; 
     echo "<td>".$student['last_name']."</td>"; 
     echo "<td>".$student['class']."</td>"; 
     echo "<td>".'<input type="checkbox" name="attendedCB">'."</td>"; 
    } 

    echo '<form>'; 
    echo 'Teacher ID: <input type="number" name=teacher_id>'; 
    echo '<button name="save" type="submit">Save Attendance</button>'; 
    echo '</form>'; 


?> 
</table> 

我有一个数据库中的“student_attendance”表,所以我想每一个学生与他/她的“出席”复选框的状态一起加入。干杯:D

+1

使复选框成为一个数组''输入类型=“复选框”名=“出席的CB []”>然后你可以循环'$ _POST ['involveCB']' – RiggsFolly

+1

规则:所有输入必须存在'

...
' – RiggsFolly

+0

使用'@'错误消音器只是让你在你的错误黑暗。在发展中你需要知道你在做什么错误 – RiggsFolly

回答

0

使用ID作为复选框的值,方法是单击获取ID值。 基于id的值,你可以做的操作(更新,删除,编辑)

echo "<td><input type='checkbox' name='attendedCB' value ='.$student["id"].'></td>"; 
+0

OP的问题还有一点...查看我的评论 – RiggsFolly

+0

可以请您说出错信息或输出问题 –

1
// loop per student 
foreach ($_POST ['student'] as $student) { 
    extract($student) // extract array (lazy) 
    // if they attended 
    if (!empty ($attendedCB)) { 
     // add them 
     $query = "INSERT INTO your_table id,first,last,class,attendedCB VALUES($id, $first, $last, $attendedCB)"; 
     // then run that query or aggregate them all into one big one and run it 
    } 
} 

还可以更改复选框名称student['attendedCB'],这应该工作。

+0

嘿,我试过了,但我确定我搞砸了.'foreach($ _POST ['student'] as $ student){ \t \t \t \t \t \t extract($ student); //提取物阵列(懒惰)//如果 \t \t \t \t \t他们参加 \t \t \t \t \t如果(!空($ attendedCB)){ \t \t \t \t \t //添加它们 \t \t \t \t \t // \t $ query =“INSERT INTO your_table id,first,last,class,trainedCB VALUES($ id,$ first,$ last,$ trainedCB)”; \t \t \t \t \t \t echo“TEST”。$ student ['id']; \t \t \t \t \t}其他{ \t \t \t \t \t \t回声 “TEST22222”。$学生[ '身份证']; \t \t \t \t \t} \t \t \t \t \t}' –

+0

它给我这些错误'(注意:未定义指数:学生在C:\ XAMPP \ htdocs中\ ApplicationDemo \ fetch_students_add.php线136 警告:在第136行的C:\ xampp \ htdocs \ ApplicationDemo \ fetch_students_add.php中为foreach()提供的无效参数)' –

+0

您是否正在发布页面? –