-1

I have a problem with my delete function. Before that, I will brief to you about 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 delete button.

My problem is, after I click button delete, it was successful delete but the dashboard_engineer2.php page doesn't display the result that same as point 3 above. The result empty.

What I want is that, after delete, the dashboard_engineer2.php will display the remaining data rows that already filtered before. Below is my code.

dashboard_engineer.php

<?php

    $smt = $conn->prepare("SELECT * FROM ot_team INNER JOIN ot_users ON ot_team.team_id = ot_users.team_id WHERE ot_users.roles_id = 7 AND ot_users.team_id <> 1 ORDER BY ot_users.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($_POST["from"], $_POST["to"], $_POST["team"])){

        $result = '';
        $query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id = '".$_POST["team"]."' AND report_date BETWEEN '".$_POST["from"]."' AND '".$_POST["to"]."' ORDER BY ot_report.report_date 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 = "'.$_POST["from"].'">';
                echo '<input type = "hidden" name = "to" value = "'.$_POST["to"].'">';
                echo '<input type = "hidden" name = "team" value = "'.$_POST["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>';

        } 

?>

delete.php

  <?php

        $report_id = $_POST['report_id'];

        $sql = "DELETE FROM ot_report WHERE report_id=:report_id";
        $query = $conn->prepare($sql);
        $query->execute(array(':report_id' => $report_id));

        header("Location: dashboard_engineer2.php");

  ?>

Can anyone help me on how to fix this?

  • Using [datatables](https://datatables.net/examples/api/select_single_row.html) would be easier than hand rolling your own. – danblack Feb 20 '20 at 02:52
  • btw thanks for being one of the few new SO posters that haven't posted code full of SQL injection vulnerabilities. Its a very pleasant change. The other ways to to ajax call from a a button and get javascript to delete the row. (not a JS or PHP writer so no code sorry) – danblack Feb 20 '20 at 03:02

3 Answers3

0

You can try my code below , sorry if it is not run delete.php

$from = $_POST["from"];
$to = $_POST["to"];
$team = $_POST["team"];
header("Location: dashboard_engineer2.php?from=$from&to=$to&team=$team");

Then change condition seem like below. dashboard_engineer2.php


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

Hope it work well .

Bình Trương
  • 380
  • 1
  • 13
0

this case is the same as the question PHP / MySQL: Display some error after successfully updated the data

that's because in dashboard_engineer2.php your code will display data from the database if the from, to, team variable is posted.

from delete.php redirect to dashboard_engineer2.php being the get method and the data that was previously posted is missing

change the delete.php code to be like this

<?php
        $report_id = $_POST['report_id'];
        $from = $_POST['from'];
        $to = $_POST['to'];
        $team = $_POST['team'];

        $sql = "DELETE FROM ot_report WHERE report_id=:report_id";
        $query = $conn->prepare($sql);
        $query->execute(array(':report_id' => $report_id));

        // so that the data previously posted is taken
        // when redirecting to the dashboard_engineer2.php page 
        // even though it is a GET method

        header("Location: dashboard_engineer2.php?from={$from}&to={$to}&team={$team}");
?>

and there was a slight change in dashboard_engineer2.php to be like this

<?php

    // change the initial conditions to be like this
    // $_REQUEST will get data if there is data in POST or GET

    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_date 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>';

        } 

?>
  • btw, can you help me to solve this problem? https://stackoverflow.com/questions/60314120/php-mysql-cannot-view-a-data-row-details – Peter Sondak Feb 20 '20 at 06:34
-1

I not PHP developer well, But I guess when you redirect on delete.php, on dashboard_engineer2.php it does not exist $_POST["from"], $_POST["to"], $_POST["team"] params.So if will return false and code on if will not excute, please debug on it. Solution is you should past it as get parameter when redirect .

Bình Trương
  • 380
  • 1
  • 13