PHP中的循环复选框
我在每个表格行中都有一个复选框,我想按下“保存”按钮,它将循环显示每个复选框: (如果选中)->输入该学生参加的数据库 else->输入该学生尚未参加的数据库 我的PHP代码:PHP中的循环复选框,php,mysql,Php,Mysql,我在每个表格行中都有一个复选框,我想按下“保存”按钮,它将循环显示每个复选框: (如果选中)->输入该学生参加的数据库 else->输入该学生尚未参加的数据库 我的PHP代码: <table border="1" cellpadding="1" width="550px" style="text-align:center;"> <th> Student ID </th> <th> First Name </th>
<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>
学生证
名字
姓
类ID
出席
我在db中有一个“student_Attention”表,因此我想添加每个学生及其“Attentied”复选框状态。干杯:通过单击获取id值的复选框,D使用id作为值。 根据id值,您可以执行操作(更新、删除、编辑)
echo”“;
另外,将复选框名称更改为
student['attendedCB']
,这应该会起作用。将复选框设置为数组
,然后您可以循环使用$\u POST['attendedCB']
规则:所有输入都必须存在..
中,使用@
错误消音器只会让您对错误保持一片黑暗。在发展过程中,你需要知道你做错了什么是的,伙计,我很抱歉我的无知触发了你:P,我自学成才(还在学习),所以你必须忍受我:)但是非常感谢这些有用的建议OP的问题还有更多。。。看看我的评论,伙计,我试过了,但我肯定把它弄糟了。foreach($\u POST['student']as$student){extract($student);//extract array(lazy)//如果他们参加了if(!empty($attendedCB)){//add them/$query=“插入你的_表id,first,last,class,attendedCB值($id,$first,$last,$attendedCB)”;echo“TEST”。$student['id'];}else{echo“TEST22222”。$student['id'];}}
它给了我这些错误(注意:未定义的索引:C:\xampp\htdocs\ApplicationDemo\fetch_students\u add.php中的student在第136行警告:为foreach()提供的参数无效)在C:\xampp\htdocs\ApplicationDemo\fetch\u students\u add.php第136行)
中,您是否要发布到该页面?是的,我的意思是确保它工作正常。当用户单击按钮时,我希望在不离开页面的情况下将条目保存到db中。
echo "<td><input type='checkbox' name='attendedCB' value ='.$student["id"].'></td>";
// 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
}
}