[英]MYSQL PHP API - Displaying & Fetching multiple rows within rows from another table
[英]Fetching Multiple rows and Store it to another table in php Mysql
我正在创建一个程序来接收学生的出勤率数据并将其存储在数据库中。我创建了student_table,该表存储了注册后的学生详细信息,例如姓名,护照号码,班级。 我创建了另一个表“ attendance_table”。 我已经在php文件中获取了学生的详细信息,并显示了带有附加列“出勤”的列,该列需要单选按钮值来标记存在或不存在。
我的问题是标记出勤后,我希望将其存储在Attenance_table中。 由于详细信息是从student_table获取的,因此我如何将其存储在另一个表中。 我是php新手。 谢谢。
// FETCHING DATA FROM STUDENT_TABLE//
<tr>
<td><?php echo $row['student_name']; ?></td>
<td><?php echo $row['Passport_no'] ?></td>
<td><?php echo $row['Teacher'] ?></td>
<td><?php echo $row['Class'] ?></td>
<td><?php echo $row['Period'] ?></td>
<td>
Present<input required type="radio" name="attendance[<?php echo $row['stud_id'] ?>]" value="Present" checked>
Absent<input required type="radio" name="attendance[<?php echo $row['stud_id']; ?>]" value="Absent" >
</td>
</tr>
<?php } ?>
//出席表//
表格attendance
表格结构
DROP TABLE IF EXISTS `attendance`;
CREATE TABLE IF NOT EXISTS `attendance` (
`at_id` int(11) NOT NULL AUTO_INCREMENT,
`stud_id` int(11) NOT NULL,
`student_name` varchar(100) NOT NULL,
`Passport_no` varchar(100) NOT NULL,
`Class` varchar(100) NOT NULL,
`Period` varchar(100) NOT NULL,
`Status` varchar(100) NOT NULL,
`Date` date NOT NULL,
PRIMARY KEY (`at_id`)
)
Dirie您需要做的是将每一行中的数据放入表格https://www.w3schools.com/tags/tag_form.asp中 。 选择单选按钮时,将触发带有jquery / javscript的提交。 或者,有一个名为“ Present”的按钮/输入(提交),它将为您提交表单。
另一个选择是使用ajax( https://www.w3schools.com/xml/ajax_php.asp )。 我个人认为,提交表格比较容易。
我怀疑您期望答案能为您提供代码,但是我不确定您是否会找人为您编写解决方案。 在线上有许多代码示例,介绍如何创建HTML表单,提交数据并将表单中的详细信息保存到数据库中。 祝你好运!
用html创建表单很容易。 只需将要提交的数据放入表单元素即可。 在此示例中, period
被置于具有隐藏属性的input tag
,我们从您的查询中分配值。
<form method="POST" action="saveattendance.php">
<table>
<?php while($row = $db->fetch(PDO::FETCH_ASSOC)): ?>
<input type="hidden" name="period" value="<?php echo $row['Period'] ?>">
<tr>
<td><?php echo $row['student_name']; ?></td>
<td><?php echo $row['Passport_no'] ?></td>
<td><?php echo $row['Teacher'] ?></td>
<td><?php echo $row['Class'] ?></td>
<td><?php echo $row['Period'] ?></td>
<td>Present<input required type="radio" name="attendance[<?php echo $row['stud_id'] ?>]" value="Present" checked>
Absent<input required type="radio" name="attendance[<?php echo $row['stud_id']; ?>]" value="Absent">
</td>
</tr>
<?php endwhile; ?>
<tr>
<td colspan="6"><input type="submit" name="attendanceData" value="Save"></td>
</tr>
</table>
</form>
为了将表单保存到数据库,我们需要一个数据库连接,这里我使用PDO mysql。
saveattendance.php
<?php
require_once 'dbconnect.php';//contains PDO connection script
if(isset($_POST['attendanceData'])){
$today = date('Y-m-d');
$period = $_POST['period'];
foreach($_POST['attendance'] as $key =>$val){
$studentid = $key;
$status = $val;
if(!empty($studentid)){
try{
$dbh->beginTransaction();
$stmt = $dbh->prepare("INSERT INTO `attendance` (`stud_id`,`Period`, `Status`,`Date`)VALUES(?,?,?,?)");
$stmt->execute(array($studentid, $period, $status, $today));
$update = $stmt->rowCount();
$dbh->commit();
}catch(PDOException $e){
$dbh->rollBack();
$error = $e->getMessage();//Print the error
exit;
}
}else{
//Student ID not there
}
}
}else{
//Form not submitted, display other useful info
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.