如何防止覆盖循环中具有选定选项值的php变量
实际上,我想将从一个数据库(学生信息)中获取的值与所选的出勤状态一起存储到另一个数据库(出勤),我想在while循环的每个迭代中存储数据。 从学生信息数据库检索到的数据正在成功存储到考勤数据库中,但它不会在每次迭代中存储特定的选定选项,它只存储最新选定选项的值,并覆盖选定选项的以前状态如何防止覆盖循环中具有选定选项值的php变量,php,html,Php,Html,实际上,我想将从一个数据库(学生信息)中获取的值与所选的出勤状态一起存储到另一个数据库(出勤),我想在while循环的每个迭代中存储数据。 从学生信息数据库检索到的数据正在成功存储到考勤数据库中,但它不会在每次迭代中存储特定的选定选项,它只存储最新选定选项的值,并覆盖选定选项的以前状态 <html> <head> </head> <body> <?php error_reporting(E_ALL ^ E_DEPRECATED); i
<html>
<head>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
include("config.php");?>
<div class="form-container">
<form method="post" action="" role="form">
<!-- <div class="container"> -->
<div class="col-lg-3">
<div class="form-group">
<?php
$qs=mysql_query("select * from student_table");
?>
<table border=1>
<?php
while($stid=mysql_fetch_row($qs))
{
?>
<tr>
<td ><?php echo $stid[0]?></td>
<td><?php echo $stid[1]?></td>
<td>
<select name="present" >
<option value=""> ---Select Attendence--- </option>
<option value="P"> Present </option>
<option value="A"> Absent </option>
</select></td>
</tr>
<?php
$stud= $stid[0]; //roll no of student from student database
$subj= $stid[1]; //name of student from student database
$date = date('Y-m-d H:i:s'); //date
if(isset($_POST['present'])){ //selected option value, but it gets overwritten and at the end displays latest value except particaular value of every iteration
$department=$_POST['present'];
$query=mysql_query("Insert into tbl_attendence (StudentRollNumber,SubjectId,Attendence,Date)VALUES('$stud','$subj','$department','$date')");
if(!$query)
{
echo mysql_error();
}
}
}
?>
</table>
</div>
</div>
<button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>
</form>
</body>
</html>
---选择出席--
目前
缺席的
因此,最终得到的是具有相同名称的多个
元素。当你这样做的时候,只有最新的一个会被发布
因此,您需要确保每个元素都有一个唯一的名称。您可以使用学生ID作为键,将其作为数组的一部分:
<select name="present['<?= $stid[0] ?>']" >
请另外,您的代码是Thanx@BizzyBob,我知道了这个概念,但它给了我这个错误“警告:为foreach()提供的参数无效”,我是php新手,您能详细说明并指导我这个警告吗?如果$\u POST['present']
不存在,您会得到这个警告。将foreach
包装在if
语句中,以确保在使用前已对其进行设置。
foreach($_POST['present'] as $stud => $present){
$date = // whatever
$subj = // whatever
$sql="INSERT INTO tbl_attendence (StudentRollNumber,SubjectId,Attendence,Date) VALUES('$stud','$subj','$present','$date')");
}