-1

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);
Hijibiji
  • 428
  • 4
  • 20
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jun 27 '20 at 12:47
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jun 27 '20 at 12:47

1 Answers1

-1

You are using the same name="report" for all radio buttons, that is the reason why only one is being checked. Use this instead

name="<?=$row['id']?>"

You will have each student result posted with the name being assigned to student id, In your process file loop through these value and update each student's result.

Ahmed Sheashaa
  • 132
  • 2
  • 11
  • Thank you for your answer but if I use id as name, it sometimes let me select both of the options (Passed and Failed) for any of the student. – Hijibiji Jun 27 '20 at 06:07
  • Make sure that you are setting the name as id for both of the inputs (not just one). – Ahmed Sheashaa Jun 27 '20 at 06:22