简体   繁体   English

提取多行并将其存储到php Mysql中的另一个表中

[英]Fetching Multiple rows and Store it to another table in php Mysql

I'm creating a program to take student attendance data and store it in the database.I have created student_table which stores student details after registration, such as Names, Passport no, Class. 我正在创建一个程序来接收学生的出勤率数据并将其存储在数据库中。我创建了student_table,该表存储了注册后的学生详细信息,例如姓名,护照号码,班级。 I have created another table "attendance_table". 我创建了另一个表“ attendance_table”。 I have fetched the student details on a php file and displayed with additional column "attendance" which takes radio button values to mark either present or absent. 我已经在php文件中获取了学生的详细信息,并显示了带有附加列“出勤”的列,该列需要单选按钮值来标记存在或不存在。

My problem is after marking the attendance I want it get stored in attendance_table. 我的问题是标记出勤后,我希望将其存储在Attenance_table中。 Since the details are fetched from student_table, how can I able to store it in another table. 由于详细信息是从student_table获取的,因此我如何将其存储在另一个表中。 I'm new to php. 我是php新手。 Thanks. 谢谢。

// 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 TABLE// //出席表//

Table structure for table attendance 表格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 what you need to do is put the data in each row into a form https://www.w3schools.com/tags/tag_form.asp . Dirie您需要做的是将每一行中的数据放入表格https://www.w3schools.com/tags/tag_form.asp中 When a radio button is selected trigger a submit with jquery/javscript. 选择单选按钮时,将触发带有jquery / javscript的提交。 Alternatively have a button/input(submit) called "Present" that will submit the form for you. 或者,有一个名为“ Present”的按钮/输入(提交),它将为您提交表单。

Another option is to use ajax ( https://www.w3schools.com/xml/ajax_php.asp ). 另一个选择是使用ajax( https://www.w3schools.com/xml/ajax_php.asp )。 I personally think submitting the form is easier though. 我个人认为,提交表格比较容易。

I suspect that you are expecting the answer to provide code for you, but I am not sure whether you will get someone who will write out a solution for you. 我怀疑您期望答案能为您提供代码,但是我不确定您是否会找人为您编写解决方案。 There is a lot of code examples online on how to create a HTML form, submit data and save the details from the Form into a database. 在线上有许多代码示例,介绍如何创建HTML表单,提交数据并将表单中的详细信息保存到数据库中。 Best of luck! 祝你好运!

Creating form in html is easy enough. 用html创建表单很容易。 Just put the data you want to submit in the form elements. 只需将要提交的数据放入表单元素即可。 In this example, period is put in the input tag with hidden attribute and we assign the value from your query. 在此示例中, 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>

In order to save the form to the database, we would need a database connection, here I used PDO mysql. 为了将表单保存到数据库,我们需要一个数据库连接,这里我使用PDO mysql。

saveattendance.php 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM