Home > OS >  approve/reject button not working properly
approve/reject button not working properly

Time:12-11

I am practicing php by making an approved/reject system, in which administrator approves/rejects student records. It works fine with one row, but with multiple rows fetched from the table either all get approved or all get rejected. What needs to be revised in the following?

 <?php
   session_start():
  ?>
<table>
        <tr>
            <th>ID</th>
            <th>Approval</th>
            <th>Picture</th>
            <th>Status</th>
</tr>
   
<?php
include "dbconn.php";
$i=1;
$query = "select * from data";
$sql = mysqli_query($conn,$query);
$count = mysqli_num_rows($sql);

   if($count>0)
   {
        while($row=mysqli_fetch_array($sql))
   {

?>
<tr>
    <td> <?php echo $row['id']; $_SESSION['stuappid']=$row['id'];?> </td>
    <td> <?php echo $row['approval']; ?> </td>
    <td> <img src ="<?php echo $row['picture']; ?>" height="100px" width="100 px">  </td>
    <td>
        <form method="post" action="">
            <button type="submit" name="approved">Approve</button>
        </form>
        <form method="post" action="">
            <button type="submit" name="rejected" >Reject</button>
        </form>
    </td>

</tr>
</table>
<?php

$i  ;
if(isset($_POST['approved']))
{
    $query2 = "update data set approval= 'Approved' where                                                                                               id='".$_SESSION['stuappid']."'";
    $sql2 = mysqli_query($conn,$query2);
    $query22= "INSERT into approved(id,status) values ('".$_SESSION['stuappid']."','Approved')";
    $sql3 = mysqli_query($conn,$query22);

}
if(isset($_POST['rejected']))
{
    $query4 = "update data set approval= 'Rejected' where id='".$_SESSION['stuappid']."'";
    $sql41 = mysqli_query($conn,$query4);
    $query5= "INSERT into rejected(id,status) values ('".$_SESSION['stuappid']."','Rejected')";
    $sql51 = mysqli_query($conn,$query5);
   
}
}
}

else{
echo "No Record";

 }

CodePudding user response:

There are a couple things that can be adjusted here. A simpler way to structure this is by adding a hidden input field with the relevant row_id value inside of your forms:

<input type="hidden" name="row_id" value="<?= $row['id']; ?>" />

(<?= is shorthand for <?php echo : What does '<?=' mean in PHP?)

Then you can move your UPDATE queries outside of the while loop to keep things simpler (and use the submitted row_id value [$_POST['row_id']] instead of the $_SESSION value, which may have been part of the issue).

Now instead of mixing the output/update logic inside the same loop, your update logic executes first if the page has been submitted, and then the table data is output below that.

<?php
    session_start():

    include "dbconn.php";

    if (isset($_POST['approved']))
    {
        $appUpdateQuery = "UPDATE data SET approval= 'Approved' WHERE id='".$_POST['row_id']."'";
        $appUpdateResult = mysqli_query($conn, $appUpdateQuery);
        $appInsertQuery = "INSERT INTO approved(id,status) VALUES ('".$_POST['row_id']."','Approved')";
        $appInsertResult = mysqli_query($conn, $appInsertQuery);
    }
        
    if (isset($_POST['rejected']))
    {
        $rejUpdateQuery = "UPDATE data SET approval= 'Rejected' WHERE id='".$_POST['row_id']."'";
        $rejUpdateResult = mysqli_query($conn,$rejUpdateQuery);
        $rejInsertQuery = "INSERT INTO rejected(id,status) VALUES ('".$_POST['row_id']."','Rejected')";
        $rejInsertResult = mysqli_query($conn, $rejInsertQuery);
    }
?>

<table>
    <tr>
        <th>ID</th>
        <th>Approval</th>
        <th>Picture</th>
        <th>Status</th>
    </tr>

<?php
    $selectQuery = "SELECT * FROM data";
    $sql = mysqli_query($conn, $selectQuery);
    $count = mysqli_num_rows($sql);

    if ($count>0)
    {            
        while ($row = mysqli_fetch_array($sql))
        {
?>
            <tr>
                <td> <?php echo $row['id']; $_SESSION['stuappid'] = $row['id']; ?> </td>
                <td> <?= $row['approval']; ?> </td>
                <td> <img src ="<?= $row['picture']; ?>" height="100px" width="100 px">  </td>
                <td>
                    <form method="post" action="">
                        <input type="hidden" name="row_id" value="<?= $row['id']; ?>" />
                        <button type="submit" name="approved">Approve</button>
                    </form>
                    <form method="post" action="">
                        <input type="hidden" name="row_id" value="<?= $row['id']; ?>" />
                        <button type="submit" name="rejected" >Reject</button>
                    </form>
                </td>
            </tr>
<?php
        }
    } else {
        echo "No Record";
    }
?>

</table> 

Based on your current queries, you can probably remove the approved and rejected columns from your approved and rejected tables if the values there will all be the same. Similarly, the approval column in data should be a boolean so you can store 0 or 1 instead of a full string.

CodePudding user response:

Add your table inside the while loop where your are running this query

   while($row=mysqli_fetch_array($sql))
  • Related