我正在使用此表单显示学生列表和单选按钮/下拉列表以显示当前缺席的内容,我想知道如何存储学生的整个数据以及出席我的数据库
<form name="insert-attendance.php" action="insert-attendance.php" method="post">
<?php
include('connection.php');
$result = $db->prepare("SELECT * FROM sc_students WHERE Student_Class='$classname' AND Student_Section='$section'");
$result->execute();
for ($i = 0; $row = $result->fetch(); $i++) {
?>
<tr>
<td><input type="text" name="stuid" value="<?php echo $row['Student_id'] ?>"></input></td>
<td><input type="text" name="stuname" value="<?php echo $row['Student_name'] ?>"></input></td>
<td><input type="text" name="stuclass" value="<?php echo $row['Student_Class'] ?>"></input></td>
<td><input type="text" name="section" value="<?php echo $row['Student_Section'] ?>"></input>
<input type="hidden" value="<?php echo date("Y-m-d"); ?>" name="attdate">
<td>
<select name="attndc">
<option value="present">PRESENT</option>
<option value="absent">ABSENT</option>
<option value="leave">LEAVE</option>
</select>
</td>
</tr>
<?php
}
?>
<input type="submit" value="submit">
</form>
这是我的insert.php文件,但它不起作用
<?php
$studentid = $_POST['stuid'];
$stuname = $_POST['stuname'];
$stuclass = $_POST['stuclass'];
$section = $_POST['section'];
$attdate = $_POST['attdate'];
$attndc = $_POST['attndc'];
include "connection.php";
$values = "$studentid, $stuname, $stuclass, $section, $attdate, $attndc";
$sql = array();
$sql[] = '("' . int($row['student_id']) . '' . mysql_real_escape_string($row['stuclass']) . '", ' . $row['section'] . ', ' . $row['attndc'] . ', ' . $row['attdate'] . ' )';
$sql = 'INSERT INTO sc_attendance (student_id, class, section, status,attendance_date) VALUES ' . implode(',', $sql);
$stmt = $db->prepare($sql);
$db->execute($sql);
?>
答案 0 :(得分:0)
您必须使用stuid[],stuname[]
之类的名称数组来输入html表单中的字段,此时所有学生的输入字段都会有相同名称stuid,stuname
。在HTML中,如果2个输入字段具有相同的名称,则最后一个字段将被视为最终输入字段,所有其他输入字段将被丢弃(实际上它们的值将被最后一个字段替换)。为了避免替换最后一个字段,必须使用带有名称的[]
,并在它们进入PHP时将它们视为数组。在这里,您的表单将如下所示:
<form name="insert-attendance.php" action="insert-attendance.php" method="post">
<?php
include('connection.php');
$result = $db->prepare("SELECT * FROM sc_students WHERE Student_Class='$classname' AND Student_Section='$section'");
$result->execute();
for ($i = 0; $row = $result->fetch(); $i++) {
?>
<tr>
<td><input type="text" name="stuid[]" value="<?php echo $row['Student_id'] ?>"></input></td>
<td><input type="text" name="stuname[]" value="<?php echo $row['Student_name'] ?>"></input></td>
<td><input type="text" name="stuclass[]" value="<?php echo $row['Student_Class'] ?>"></input></td>
<td>
<input type="text" name="section[]" value="<?php echo $row['Student_Section'] ?>"></input>
<input type="hidden" value="<?php echo date("Y-m-d"); ?>" name="attdate[]">
<td>
<select name="attndc[]">
<option value="present">PRESENT</option>
<option value="absent">ABSENT</option>
<option value="leave">LEAVE</option>
</select>
</td>
</tr>
<?php
}
?>
<input type="submit" value="submit">
</form>
现在在insert.php
中,您必须将输入字段视为数组,以便您可以获取所有学生的数据并将其插入数据库表中:
<?php
include "connection.php";
$total = count($_POST['stuid']);
for($i=0;$i<$total;$i++){
$studentid = $_POST['stuid'][$i];
$stuname = $_POST['stuname'][$i];
$stuclass = $_POST['stuclass'][$i];
$section = $_POST['section'][$i];
$attdate = $_POST['attdate'][$i];
$attndc = $_POST['attndc'][$i];
$sql = $db->prepare("INSERT INTO sc_attendance (student_id, class, section, status,attendance_date) VALUES (?, ?, ?, ?, ?)");
$sql->bind_param($studentid, $stuclass, $section, $attdate, $attndc);
$db->execute($sql);
}
?>
您在sql语句和查询中犯了很多错误。我已经纠正了你。