-2

I have a table where it shows number of rows of specific dates. I want after deletion of a row, to get back to the same page and show the same rows of the same date. before delete enter image description here after I delete the row that what happened enter image description here

include file

<!-- Packages section -->
<li class="nav-item">
  <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapsePackage" aria-expanded="true" aria-controls="collapsePackage">
    <i class="fas fa-fw fa-wrench"></i>
    <span>Packages</span>
  </a>
  <div id="collapsePackage" class="collapse" aria-labelledby="headingPackage" data-parent="#accordionSidebar">
    <div class="bg-white py-2 collapse-inner rounded">
      <h6 class="collapse-header">Package Posts</h6>
      <a class="collapse-item" href="packages.php">View Packages</a>
      <a class="collapse-item" href="new_package.php">Add Package</a>
      <a class="collapse-item" href="req_package.php">requested Package</a>
    </div>
  </div>
</li>

connect_package // Delete a post

** if(isset($_REQUEST['delete'])){
    $id = $_REQUEST['id'];
    
    $sql = "DELETE FROM package_post WHERE id = $id";
    // mysqli_query($conn, $sql);
    
    if(mysqli_query($conn, $sql)){
      header("Location: packages.php?info=delete");
  } else{
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
  } 
    mysqli_close($conn);
    
  }**
ر ع
  • 1
  • 2
  • 1
    You're already on the same page, no need to go back. Make sure the deletion is before any retrieval, and add the message where you need it, just like the "post has been added successfully" message – aynber Jul 22 '22 at 15:35
  • 1
    **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Jul 22 '22 at 16:03
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use it again. – ADyson Jul 22 '22 at 16:03
  • Also. add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before your `mysqli_connect()` (or `new mysqli()`) command, and this will ensure that errors with your SQL queries are reported correctly to PHP automatically. This saves you having to write repetitive error handling code after every mysqli command. Also you should never directly echo SQL errors to the browser - in a live system this would be a security problem. Instead, just let PHP handle the exception, and then it will either log it or display it as per its configuration in that environment. – ADyson Jul 22 '22 at 16:04

1 Answers1

0

I think that the page is not redirecting correctly because of some output already sent to the browser. My suggestion is to check if there is an newline or a space before opening the "php tag" (<?php) o closing it (?>). If so, try to remove spaces and other non-printable characters.

Also you should check that eventually included files, before the header("Location: ..."), don't send output to the browser: in this case you should move the included file after the redirect, if possible.

Another thing you should consider is to use output buffering and exit() the script just after the header() https://www.php.net/ob_start

Your php script should look like:

<?php
ob_start();
// do something here... even "echoes" or "includes"

if(isset($_REQUEST['delete'])){
    $id = $_REQUEST['id'];
    
    $sql = "DELETE FROM package_post WHERE id = ?";
    $statement = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($statement, 'i', $id); // $id is supposed to be an integer, change the 2nd param to 's' if it's not a number
    
    mysqli_stmt_execute($statement);
    
    header("Location: packages.php?info=delete");
    exit(); // mysqli connection will be implicitly closed
}

// do something else....

ob_end_flush(); // Output is finally sent to the browser
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
DanieleV
  • 11
  • 3