PHP / MySQL: Cannot view a data row details

exception or error:

Ihave a problem when i want to view a data row in details. Below is the flow of the system:

1) At dashboard_engineer.php, there’s 3 select options which is team, time from and time to.

2) user need to select 3 select option and press button “Search” to display the result.

3) The result will display at dashboard_engineer2.php.

4) The result will display all data rows and each row contains one View button which can view a data row in detail.

My problem is only in the first data row. When I click button View’, it will redirect to dashboard_engineer2.php with empty data. But for the second data and next, I can view the details of selected data.

This only happen only at the first data that display after filtered. Below is my code.

dashboard_engineer.php

    <?php

        $smt = $conn->prepare("
SELECT * 
  FROM ot_team t 
  JOIN ot_users u
    ON t.team_id = u.team_id 
 WHERE u.roles_id = 7 
   AND u.team_id <> 1 
 ORDER 
    BY u.fullname ASC
");
        $smt->execute();
        $data = $smt->fetchAll();

    ?>

    <form method = 'post' action = 'dashboard_engineer2.php' >
    <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" value="Search" class="btn btn-primary" >Send</button><td>
    </form>

    <script>
    $(document).ready(function(){
    $.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd'
    });
    $(function(){
        $("#from").datepicker().attr("autocomplete", "off");;
        $("#to").datepicker().attr("autocomplete", "off");;
    });

    });
    </script>

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 r
  LEFT
  JOIN ot_users u
    ON r.badgeid = u.badgeid 
 WHERE u.team_id = '".$team."' 
   AND report_date BETWEEN '".$from."' AND '".$to."' 
 ORDER 
    BY r.report_id DESC
";

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

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

        echo'

        <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 = "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);

            $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["task_name"].'</td>';
                echo '<td align="center"><strong>'.$row["report_status"].'</strong></td>';
                echo '<td align="center">';
                echo '<form action = "view_task/view_task.php" method = "post">';
                echo '<input type = "hidden" name = "report_id" value = "'.$report_id.'">';
                echo '<button type = "submit" class="btn-primary">View</button>';
                echo '</form>';

                echo "<form action = 'delete.php' method = 'post' onClick=\"return confirm('Do you want to remove team?')\">";
                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">Delete</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 = "23%">Task Name</th>
            <th width = "10%">Status</th>
            <th width = "7%">Action</th>
            </tr>
            <tr>
            <td colspan="5">No report found</td>
            </tr>';
        }
        echo '</body></table></div></div>';

        } 

?>

view_task.php

<?php

require_once "../../../../config/configPDO.php";
require_once "../../../../config/check.php";

    if (isset ($_POST['report_id'])) {
        $report_id = $_POST['report_id'];
    }else if (isset ($_GET['report_id'])) {
        $report_id = $_GET['report_id'];
    }else {
        die ("ID NOT DEFINED");
    }

$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"];
    $task_name = $row["task_name"];

}

?>

<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> //line 50
<input type="hidden" name="report_id" value="<?php echo $report_id ?>"> 
<td><input type="submit" class="btn btn-primary btn-block" value = "Save" onclick="confirm('Are you sure?')"></td>
</form>

Can anyone help?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *