我正在尝试制作考勤表。在我的代码中,我从现有数据库中检索名称并使用出勤选择下拉列表填充到 html 表中。填充下拉列表后,html 表中的所有数据会自动保存到新数据库中,而无需选择提交按钮。并且考勤状态在数据库中显示为空值,但其他字段保存成功。请仔细阅读我的代码并提出您宝贵的建议以纠正问题。
<form enctype="multipart/form-data" name="f1" method="POST" ACTION="<?php echo $PHP_SELF;?>">
//call from the previous php page
<? $back_page ="recordentry.php";
$date=$_POST['date'];
$course=$_POST['course'];
$period=$_POST['period'];
$batch=$_POST['batch'];
$subject=$_POST['subject'];
if(empty($_POST['other']))
{
$faculty=$_POST['faculty'];
}
else
{
$faculty=$_POST['other'];
}
?>
<p>Current Date & Time : <? echo $date ?></p>
<?
$db="contact";
//mysql_connect(localhost,$_POST['username'],$_POST['pass']);
$link = mysql_connect("localhost", "root", "");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result=mysql_query("SELECT name,uic FROM student where batch='$batch' AND course='$course' order by name")
or die("SELECT ERROR: ".mysql_error());
$userinfo = array();
echo "<table border='1' align='center'><tr><th>Name</th><th>Unique Identification Code</th><th>Attendance(select only the Absentees)</th></tr>";
while($row = mysql_fetch_array($result)){
//Display the results in different cells
echo "<tr><td align=center>" . $row['name'] . "</td><td align=center>" . $row['uic'] . "</td><td align=center><select name='attendance' style='background-color:#FFC'>
<option>Present
<option>Absent
</select></td></tr>";
//$userinfo = array('name' => $row['name'], 'uic' => $row['uic'], 'attendance' => $row['attendance']);
$row_name=$row['name'];
$row_uic=$row['uic'];
$row_attn=mysql_real_escape_string($_POST['attendance']);
$userinfo[] = array("name"=> $row_name , "uic"=> $row_uic, "a_status"=> $row_attn);
}
//echo"<tr><td><input type='submit' value='Submit' name='submit_button' />";
echo"</table>";
mysql_close($link);
//form HAS been submitted
?>
<table align="center">
<tr><td><input type="submit" value="Submit" name="submitbtn" ></td><td><input type="reset" value="reset"></td></tr>
</table>
</form>
<?
if($_SERVER['REQUEST_METHOD']=='POST')
{
foreach ($userinfo as $value)
{
$name = $value[name];
$uic = $value[uic];
$a_status = $value[a_status];
$db="contact";
$link = mysql_connect("localhost", "root", "");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());
$result=mysql_query("INSERT INTO attendance(date, course, period, batch, subject, faculty, name, uic, attendance) VALUES
('$date', '$course', '$period', '$batch', '$subject', '$faculty', '$name', '$uic', '$a_status')")
or die("Insert Error: ".mysql_error());
mysql_close($link);
}
}
?>
除考勤外的所有数据都成功保存到数据库中。出勤给出空值。同样,在单击提交按钮之前保存数据。请帮忙。