Home » Php » PHP / MySQL: Display some error after successfully updated the data

PHP / MySQL: Display some error after successfully updated the data

Posted by: admin February 25, 2020 Leave a comment

Questions:

Currently, I create a system using CRUD function. I have some problems with my update function. The problem is, it will display the error at view_task.php like:

1) Undefined index: report_id at line 7
2) Undefined variable: task_name at line 50

The problem is, when I check the database, the data has been updated.

Below is my current code:

dashboard.php

   echo "<form method = 'post' action = 'view_task/view_task.php'>";
     echo "<input type = 'hidden' name = 'report_id' value = '".$report_id."'>";
     echo "<button type = 'submit' class='btn-primary'>View</button>";
   echo "</form>";

view_task.php

<?php

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

  $report_id = $_POST['report_id']; //line 7

  $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>

update_task_name.php

<?php

    require_once '../../../../config/configPDO.php';

    $update = "UPDATE ot_report SET task_name = :task_name WHERE report_id = :report_id";
    $stmt = $conn->prepare($update);
    $stmt->bindParam(':task_name', $_POST['task_name']);
    $stmt->bindParam(':report_id', $_POST['report_id']);
    $stmt->execute();

    class Result {}
    $response = new Result();
    $response->result = 'OK';
    $response->message = 'Update successful';
    header("Location: view_task.php");

?>

Can anyone help me to fix this problem? Thanks

How to&Answers:

in the view_task.php page the $ _POST [‘report_id’] section is not defined after the data update, because in update_task_name.php you use a redirect header (“Location: view_task.php”) which becomes the GET method

try to update

update_task_name.php

header ("Location: view_task.php?report_id=". $ _ POST ['report_id'])

view_task.php //line 7

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

Answer:

The issue is that the variable that is being called isn’t yet defined (in your case, when using POST).

   if(isset($_POST['report_id'])) {
      $report_id = $_POST['report_id']; //line 7

      $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"];

      }
    }

And for the task_name, you can do the same by wrapping it in an isset:

   if(isset($_POST['report_id'])) {
    $update = "UPDATE ot_report SET task_name = :task_name WHERE report_id = :report_id";
    $stmt = $conn->prepare($update);
    $stmt->bindParam(':task_name', $_POST['task_name']);
    $stmt->bindParam(':report_id', $_POST['report_id']);
    $stmt->execute();

    class Result {}
    $response = new Result();
    $response->result = 'OK';
    $response->message = 'Update successful';
    header("Location: view_task.php");
}