这个函数是向数据库中插入值。首先,它会列出“$class”类(class)的所有学生。界面如下:
//我没法发图片,因为我名气不大。但是,界面是它将列出所有带有“NO”、“Birth No”、“Student Name”、“Attendance”列的学生。在考勤栏中,会显示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
这样的单选按钮,这将创建一个 $_POST['a1']
如果它处于事件状态,则以“mc”作为值。但是$_POST['a1']
不是数组,它只包含这个单个值!
所以你应该退出 foreach 循环并开始工作 $_POST['a'.$id]
作为你的$status
.在其他作品中,制作一个
$status = $_POST['a'.$id] // by the way you do not need .'' in the end of the key
而不是你的 foreach 循环。
此外,您的脚本似乎不安全。您没有使用 csrf-、xss- 和 sql-injection-protection。您真的真的需要了解 php 应用程序(csrf、xss、sql 注入(inject))中的安全性。这非常重要!
关于php - 虽然循环使用单选按钮为许多用户将值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724926/