-3

Delete Button Code

As you can see I'm trying to redirect the user index.php after deleting data. How can I do that?

Here is the action form:

<form action="scripts.php" method="POST">
      <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
      <input type="hidden" name="del_new_name" value="<?php echo $row['new_name']; ?>">
      <button type="submit" name="delete_new_name" class="btn btn-danger">Delete</button>
</form>

Post Method Code

Here is the post method code on scripts.php file:

//scripts.php File:
        
        if (isset($_POST['delete_new_name'])) {
            $id = $_POST['delete_id'];
            $new_name = $_POST['del_new_name'];
        
        
            $query = "DELETE FROM uploaded_files WHERE id='$id' ";
            $query_run = mysqli_query($conn, $query);
        
            if ($query_run) {
                unlink("uploads/" . $new_name);
                $_SESSION['status'] = "Data Deleted Successfully";
                header('location: index');
                
                
            } else {
                $_SESSION['status'] = "Data Not Deleted";
                header('location: index.php');
                
            }
        }

Session Status

Here is the Session Status code on the index.php

<?php
      if (isset($_SESSION['status']) && $_SESSION != '') {
            ?>
              <div class="alert alert-warning alert-dismissible fade show" role="alert">
                <strong> Hey! </strong> <?php echo $_SESSION['status']; ?>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>

            <?php
              unset($_SESSION['status']);
            }
            ?>

It goes scripts.php file and deletes the data from the folder and database, after deleting it, it doesn't redirect index.php. I think I'm missing something plz help me out

  • 1
    Should `header('location: index');` actually say `header('location: index.php');` just like the other example in the same script? You said you wanted to redirect to "index.php", and "index" isn't the same as "index.php". – ADyson Aug 09 '21 at 10:23
  • 1
    Be warned that your `DELETE` query is widely open for SQL injection. Also, what have you tried to resolve your problem? Where are you stuck? – Nico Haase Aug 09 '21 at 10:24
  • Thanks for your edit, but...did you try my suggestion? It seems like the most likely cause of the issue. – ADyson Aug 09 '21 at 10:36
  • On the redirect page. After deleting the data it holds on the scripts.php it doesn't redirect the index.php file – Shakij Mahamud Aug 09 '21 at 10:38
  • Letting you know – Shakij Mahamud Aug 09 '21 at 10:40
  • @ShakijMahamud so did that help? If not, then what error are you seeing when it fails to redirect? Have you switched on PHP error reporting? – ADyson Aug 09 '21 at 10:56
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Aug 09 '21 at 18:39

1 Answers1

-1

Do not use the white space. Please check the below code.

//scripts.php File:

    if (isset($_POST['delete_new_name'])) {
        $id = $_POST['delete_id'];
        $new_name = $_POST['del_new_name'];
    
    
        $query = "DELETE FROM uploaded_files WHERE id='$id' ";
        $query_run = mysqli_query($conn, $query);
    
        if ($query_run) {
            unlink("uploads/" . $new_name);
            $_SESSION['status'] = "Data Deleted Successfully";
            header('location:index.php'); // Changed here index to index.php
            
            
        } else {
            $_SESSION['status'] = "Data Not Deleted";
            header('location:index.php');
            
        }
    }

Also removed the white space from header('location: index.php'); to header('location:index.php');

  • Facing the same problem. Let me add a demo link – Shakij Mahamud Aug 09 '21 at 10:49
  • @vikashkumar No, whitespace is not significant in the syntax. – ADyson Aug 09 '21 at 10:51
  • Then I think issue with unlink("uploads/" . $new_name); Please comment it and check. – Vikash Kumar Aug 09 '21 at 10:53
  • @vikashkumar why not suggest to OP that they enable PHP error reporting, instead of just commenting / uncommenting lines or changing syntax at random. It's usually easier to actually get error info than to just guess. Also we don't know yet if they actually made the change from "index" to "index.php"...according to the comments in the main thread they are still waiting to report back on whether that helped. – ADyson Aug 09 '21 at 10:53
  • Please add some explanation to your answer such that others can learn from it. What did you change, and why? – Nico Haase Aug 09 '21 at 11:14
  • @NicoHaase I have changed the header('location: index'); to header('location:index.php'); and removed the white-space. Because IN Core PHP we need to add extension after page name if you are not using any htacess to hide extension. also might be white space issue in header location. But in my latest comment i suggest him to comment the unlink method and check. – Vikash Kumar Aug 09 '21 at 11:50
  • 1
    Please add all clarification **to your answer** by editing it – Nico Haase Aug 09 '21 at 11:51
  • `also might be white space issue in header location`... still not necessary or relevant, no. It would be trivial for you to test this yourself. – ADyson Aug 09 '21 at 15:37
  • it's a hosting issue. I don't know how? It works perfectly on infinityfree hosting. – Shakij Mahamud Aug 09 '21 at 16:49
  • @ShakijMahamud If you don't know how then do some debugging..we don't know how either, because we don't have any technical information. – ADyson Aug 09 '21 at 20:45