PHP / MYSQL: Cannot view first data row after filter at PHP

Peter Davidson :

I have a really weird issue in my code. To be short, I created a system that has a dashboard called dashboard_engineer.php. This dashboard will display only the first 30 data rows of the MySQL database. This dashboard also contains 3 filtered inputs which are:

  • team
  • date from
  • date to

Users can use this filter to find their exact data rows. The user just fills in the input and presses the button Search, and this will redirect the user to dashboard_engineer2.php.

In this dashboard, all the filtered data rows will be displayed. Each data row contains a View and a Remove button. The View button is used to view the details of the data row.

My problem is, at the filtered data, the details in the first data rows that is displayed are empty when the View button is clicked. But, the details can be viewed for the second, third and following data rows as the user clicks on the View button. When I inspected the first row's View button, the ID of that button is correct.

Any suggestions on how to resolve this will be appreciated.

Here's my current code

dashboard_engineer.php

  <form method = 'post' action = 'dashboard_engineer2.php' target="_blank">
    <td width="40%">
      <select class="form-control"  name="team" id="team" required>
        <option value="">Please select...</option>
        <?php foreach ($data as $row2): ?>
          <option value="<?php echo $row2["team_id"]; ?>"><?php echo $row2["fullname"]; ?></option>
        <?php endforeach ?>
      </select>
    </td>
    <td width="1%"></td>
    <td width="20%"><input type="text" name="from" id="from" class="form-control" placeholder="From" required></td>
    <td width="1%"></td>
    <td width="20%"><input type="text" name="to" id="to" class="form-control" placeholder="To" required></td>
    <td width="1%"></td>
    <td width="10%"><button type="submit" class="btn-primary" >Search</button><td>
  </form>
  </tr>
</table><br>
<div class="row" style='height: 300px; overflow-y: scroll;'>
  <div class="col-lg-12 grid-margin stretch-card">
    <?php

    $query = $conn->query("SELECT TOP 30 * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id <> 1 AND ot_report.status = 'yes'  ORDER BY ot_report.report_id DESC");
    $query -> execute();
    $results = $query -> fetchAll(PDO::FETCH_OBJ);
    if(empty($results)){

      echo "<table class = 'table-bordered' width ='100%'>";
      echo "<thead>";
        echo "<tr>";
            echo "<th width = '5%'>id</th>
            <th width = '17%'>Date</th>
            <th width = '27%'>Officer/ Asst. Engineer</th>
            <th width = '32%'>Task Name</th>
            <th width = '12%'>Status</th>
            <th width = '7%'>Action</th>
          </tr>
        </thead>
        <tbody >
        <tr>
        <td colspan='8'>No report at this moment</td>
        </tr>
        </tbody>
        </table>";
    }else{

          echo "<table class = 'table-bordered' width ='100%'>";
          echo "<thead>";
            echo "<tr>";
                echo "<th width = '5%'>id</th>
                <th width = '17%'>Date</th>
                <th width = '27%'>Officer/ Asst. Engineer</th>
                <th width = '32%'>Task Name</th>
                <th width = '12%'>Status</th>
                <th colspan = '2' width = '7%'>Action</th>
              </tr>
            </thead>
            <tbody >";

            $query = $conn->query("SELECT TOP 30 * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id <> 1 AND ot_report.status = 'yes' ORDER BY ot_report.report_id DESC");
            while($row = $query->fetch(PDO::FETCH_ASSOC)){
            $status=$row['report_status'];
            if($status=="Pending")
            {
              $color="color:blue";
            }
            else 
            {
                $color="color:green";
            }

            $report_id = $row['report_id'];
            $datereport = $row['report_date'];
            $datereport2 = strtotime($datereport);
            $report_date = date('d M Y', $datereport2);
            $fullname = $row['fullname'];
            $task_name = $row['task_name'];


            echo "<tr>";

              echo "<td>". $report_id. "</td>";
              echo "<td>". $report_date . "</td>";
              echo "<td>". $fullname . "</td>";
              echo "<td>". $task_name . "</td>";

              echo "<td align='center' style='$color'><strong>". $status . "</strong></td>";
              echo "<td align='center'>";
                echo "<form method = 'post' action = 'view_task/view_task.php' target='_blank'>";
                  echo "<input type = 'hidden' name = 'report_id' value = '".$report_id."'>";
                  echo "<button type = 'submit' class='btn-primary'>View</button>";
                echo "</form>";

              echo "</td>";
              echo "<td align='center'>";
                echo "<form method = 'post' action = 'remove.php' onClick=\"return confirm('Do you want to remove this report?')\">";
                  echo "<input type = 'hidden' name = 'report_id' value = '".$report_id."'>";
                  echo "<button type = 'submit' class='btn-danger'>Remove</button>";
                echo "</form>";
              echo "</td>";
            echo "</tr>";
            }

            echo "</tbody>";
          echo "</table><br>";  
    }
    ?>

dashboard_engineer2.php

<?php

if(isset($_REQUEST["from"], $_REQUEST["to"], $_REQUEST["team"])){

  $from = $_REQUEST['from'];
  $to   = $_REQUEST['to'];
  $team = $_REQUEST['team'];

  $result = '';
  $query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id = '".$team."' AND report_date BETWEEN '".$from."' AND '".$to."' ORDER BY ot_report.report_id DESC";

  $sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
  $sql -> execute();

    if($sql->rowCount() > 0){

      echo'

      <form method="post" action="">
      <div><a href="../pdf.php?from='.$from.'&to='.$to.' &team='.$team.'" target="_blank"><u>PDF View</u></a></div><br>
      <div class="row" style="height: 300px; overflow-y: scroll;">
      <div class="col-lg-12 grid-margin stretch-card">
      <table class = "table-bordered" width = "100%">
      <thead>
      <tr>
      <th width = "10%"><input type="checkbox" id="checkAl"> All</th>
      <th width = "3%">id</th>
      <th width = "15%">Date</th>
      <th width = "25%">Supervisor</th>
      <th width = "30%">Task Name</th>
      <th width = "10%">Status</th>
      <th colspan = "2" width = "7%">Action</th>
      </tr>
      </thead>
      <tbody>';

        $i=0;

        while($row = $sql->fetch(PDO::FETCH_ASSOC)){

          $datereport = $row['report_date'];
          $datereport2 = strtotime($datereport);
          $report_date = date('d M Y', $datereport2);

          $status=$row['report_status'];

          if($status=="Pending"){
            $color="color:blue";
          }
          else {
            $color="color:green";
          }

          $report_id = $row["report_id"];

          echo'<tr>';
            echo '<td><input type="checkbox" id="checkItem" name="check[]" value='.$row['report_id'].'></td>';
            echo '<td>'.$report_id.'</td>';
            echo '<td>'.$report_date.'</td>';
            echo '<td>'.$row["fullname"].'</td>';
            echo '<td>'.$row["task_name"].'</td>';
            echo '<td align="center" style='.$color.'><strong>'.$status.'</strong></td>';
            echo '<td align="center">';
            echo '<form action = "view_task/view_task.php" method = "post" target="_blank">';
            echo '<input type = "hidden" name = "report_id" value = "'.$report_id.'">';
            echo '<button type = "submit" class="btn-primary">View</button>';
            echo '</form>';
            echo '</td>';
            // echo '<a class="btn-view btn-primary btn-sm" href="view_task/view_task.php?report_id='. $report_id .'" data-toggle="tooltip" >View</a></td>';
            echo '<td align="center">';
            echo "<form action = 'remove2.php' method = 'post' onClick=\"return confirm('Do you want to remove this reports?')\">";
              echo '<input type = "hidden" name = "from" value = "'.$from.'">';
              echo '<input type = "hidden" name = "to" value = "'.$to.'">';
              echo '<input type = "hidden" name = "team" value = "'.$team.'">';
              echo '<input type = "hidden" name = "report_id" value = "'.$report_id.'">';
              echo '<button type = "submit" class="btn-danger">Remove</button>';
            echo '</form>';

            echo '</td>';
          echo '</tr>';
          $i++;

        }
          echo '<tr>';
            echo '<td><p align="center"><button type="submit" class="btn-danger btn-sm" name="save">DELETE</button></p></td>';
          echo '</tr>';
          echo '</form>';

      }
      else
      {
        echo '
        <table class = "table-bordered" width = "100%">
        <thead>
        <tr>
        <th width = "5%">id</th>
        <th width = "12%">Date</th>
        <th width = "29%">Supervisor</th>
        <th width = "23%">Task Name</th>
        <th width = "7%">From</th>
        <th width = "7%">To</th>
        <th width = "10%">Status</th>
        <th width = "7%">Action</th>
        </tr>
        <tr>
        <td colspan="8">No report found</td>
        </tr>';
      }
      echo '</body></table></div></div>';

      } 

      ?>

viewtask/view_task.php

<?php

  include("../../../../config/configPDO.php");
  include("../../../../config/check.php");


  $team_n = $_SESSION['team_n'];

  if (isset ($_POST['report_id'])) {
      $report_id = $_POST['report_id'];
  }else if (isset ($_GET['report_id'])){
      $report_id = $_GET['report_id'];
  }else {
      exit ("<script type='text/javascript'>alert('Not allowed!'); location.href = '../dashboard_engineer.php';</script>");
  }

  $sql = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid LEFT JOIN ot_team ON ot_team.team_id = ot_users.team_id WHERE report_id = :report_id";
  $query = $conn->prepare($sql);
  $query->execute(array(':report_id' => $report_id));
  while($row = $query->fetch(PDO::FETCH_ASSOC)){

      $report_id = $row["report_id"];
      $report_date = $row["report_date"];
      $task_name = $row["task_name"];
      $photo_before = $row["photo_before"];
      $photo_after = $row["photo_after"];
      $ot_start = $row["ot_start"];
      $ot_end = $row["ot_end"];
      $time_photo_before = $row["time_photo_before"];
      $time_photo_after = $row["time_photo_after"];
      $report_status = $row["report_status"];

  }

?>

 <table align="center" width="100%">
    <tr>
      <td width="20%"><b>Date Report</b></td>
      <td width="80%" colspan='3'><?php echo $report_date; ?></td>
    </tr>
    <tr>
    <form action="update_task_name.php" method="POST" >
      <td><b>Task Name</b></td>
      <td colspan = '2'><input type="text" class="form-control" name="task_name" value="<?php echo $task_name; ?>"/></td>
      <input type="hidden" name="report_id" value="<?php echo $report_id ?>"> 
      <td><button type="submit" class="btn btn-primary btn-block" onclick="confirm('Are you sure?')">Save</button></td>
    </form>
    </tr>
    <form action="update_photo_before.php" method="post" id="form" enctype="multipart/form-data">
    <tr>
      <td rowspan = '3'><b>Before Task</b></td>

      <?php
        if(!isset($photo_before) || empty($photo_before)) {
            echo "<td colspan='2' rowspan='2'><img id='output_before' src='../../../../images/faces/noimage.png'/ style='width:220px;height:300px;'></td>";
          }else{ 
                  echo "<td colspan='2' rowspan='2'><img id='output_before' src='$photo_before'/ style='width:220px;height:300px;'></td>"; 
          }
      ?>

      <td colspan ='2'><input type="file" name="uploadFile" value="" accept="image/*" onchange="preview_before(event)"/></td>
    </tr>
    <tr>
      <input type="hidden" name="report_id" value="<?php echo $report_id ?>"> 
      <td><input type="submit" name="submit" value="Upload" onclick="confirm('Are you sure?')"/></td>

    </tr>
    </form>
    <tr>
danblack :

SELECT TOP 30 * FROM isn't a MySQL syntax. Check your server error log for errors. SELECT ... ORDER BY ot_report.report_id DESC LIMIT 30 is probably want you want.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193846&siteId=1