実際には、あるデータベース(学生情報)から取り出した値を他のデータベース(出席者)に保存し、出席状況を選択して保存したいwhileループのすべての反復で。 学生情報データベースから取得したデータは、出席データベースに正常に格納されていますが、選択された最新のオプションの値のみを保存し、選択したオプションの前の状態を上書きします。ループ内で選択されたオプションの値を持つphp変数の上書きを防ぐ方法
<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>
[mysql_関数を使用して停止]してください(http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。また、あなたのコードは[SQLインジェクション]にオープンしています(http://stackoverflow.com/documentation/php/5828/pdo/2685/preventing-sql-injection-with-parametrized-queries) – Machavity