出席簿を作ろうとしています。私のコードでは、既存のデータベースから名前を取得し、出席選択ドロップダウンを使用して html テーブルに入力します。ドロップダウンに入力した後、送信ボタンを選択しなくても、html テーブルのすべてのデータが新しいデータベースに自動的に保存されます。さらに、出席状況はデータベースで null 値を示していますが、他のフィールドは正常に保存されています。私のコードを調べて、問題を修正するための貴重な提案をしてください。
<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);
}
}
?>
出席を除くすべてのデータがデータベースに正常に保存されました。出席は null 値を返します。ここでも、送信ボタンをクリックする前にデータを保存します。助けてください。