I'm trying to get all the students test reports of a specific date in a table and display whether they have passed or failed in a radio button for each student. From the table, I'm trying to update their test result (passed/failed). But in my query, I'm getting only result of either one of the students and when I update, only that specific student gets updated, none other does.
Here is my index.php file. In the table only one students report radio button is active:
<?php
$mysqli = new MySQLi( 'localhost', 'root', '', 'students' )or die( mysql_error( $mysqli ) );
$result = $mysqli->query( "SELECT * FROM class_nine WHERE test_date = '2020-06-20' ORDER BY ID DESC" )or die( $mysqli->error );
?>
<div class="container">
<div class="row">
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id;?>">
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Report</th>
</tr>
</thead>
<?php
while ( $row = $result->fetch_assoc() ):
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['name'];?></td>
<td><label class="radio-inline">
<input type="radio" name="report" value="Passes" <?=$row['report'] == "Passed" ? "checked" : ""?>>
Passed</label>
<label class="radio-inline">
<input type="radio" name="report" value="Failed" <?=$row['report'] == "Failed" ? "checked" : ""?>>
Failed</label></td>
</tr>
<?php endwhile; ?>
</table>
<button type="submit" name="update">Update</button>
</form>
</div>
</div>
This is my process file:
<?php
session_start();
$mysqli = new MySQLi( 'localhost', 'root', '', 'students' )or die( mysql_error( $mysqli ) );
$id = 0;
$report = '';
if ( isset( $_POST[ 'update' ] ) ) {
$id = $_POST[ 'id' ];
$report = $_POST[ 'report' ];
$mysqli->query( "UPDATE class_nine SET
report = 'report'
WHERE id = $id" )or die( $mysqli->error );
header( "location: index.php" );
}
?>
This is how my MySQL table is structured:
CREATE TABLE class_nine(
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(1) NOT NULL
,test_date DATE NOT NULL
,report VARCHAR(6)
);
INSERT INTO class_nine(id,name,test_date,report) VALUES (23,'A','2020-06-20','Passed');
INSERT INTO class_nine(id,name,test_date,report) VALUES (33,'B','2020-06-20',NULL);
INSERT INTO class_nine(id,name,test_date,report) VALUES (35,'C','2020-06-20','Failed');
INSERT INTO class_nine(id,name,test_date,report) VALUES (45,'D','2020-06-22',NULL);