Php 如何将考勤数据从表单导入mysql数据库?
我用这个表格来显示学生名单,并用单选按钮/下拉列表来放置当前缺席的东西,我想知道如何将学生的全部数据以及出勤情况存储到我的数据库中Php 如何将考勤数据从表单导入mysql数据库?,php,mysql,arrays,Php,Mysql,Arrays,我用这个表格来显示学生名单,并用单选按钮/下拉列表来放置当前缺席的东西,我想知道如何将学生的全部数据以及出勤情况存储到我的数据库中 <form name="insert-attendance.php" action="insert-attendance.php" method="post"> <?php include('connection.php'); $result = $db->prepare("SELECT * FROM
<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>
您必须使用名称数组,如stuid[],stuame[]
在html表单中输入字段,现在所有学生的输入字段都将有相同的名称stuid,stuame
。在HTML中,如果两个输入字段具有相同的名称,则最后一个字段将被视为最终输入字段,所有其他输入字段将被丢弃(事实上,它们的值将被最后一个字段替换)。为了避免最后一个字段被替换,您必须使用带有名称的[]
,并在它们进入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>
您在sql语句和查询中犯了许多错误。我已经为您更正了。您必须使用名称数组,如stuid[],stuame[]
在html表单中输入字段,现在所有学生的输入字段都将使用相同的名称stuid,stuame
。在HTML中,如果两个输入字段具有相同的名称,则最后一个字段将被视为最终输入字段,所有其他输入字段将被丢弃(事实上,它们的值将被最后一个字段替换)。为了避免最后一个字段被替换,您必须使用带有名称的[]
,并在它们进入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>
您在sql语句和查询中犯了许多错误。我已经帮你改正了
<?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);
}
?>