简体   繁体   中英

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. I have created another 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.

My problem is after marking the attendance I want it get stored in attendance_table. Since the details are fetched from student_table, how can I able to store it in another table. I'm new to 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

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 . When a radio button is selected trigger a submit with jquery/javscript. Alternatively have a button/input(submit) called "Present" that will submit the form for you.

Another option is to use 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. Best of luck!

Creating form in html is easy enough. 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.

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

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
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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