2

Hi Guys trying to find the mistake page not changing to index.php.

Not Redirecting to landing page everything else works fine.

Record is deleted from db, page with delete yes/no also ok

    <?php
    // Process delete operation after confirmation
    if(isset($_POST["ID"]) && !empty($_POST["ID"])){
        // Include config file
        require_once 'config.php';

        // Prepare a select statement
        $sql = "DELETE FROM cv WHERE ID = ?";

        if($stmt = mysqli_prepare($link,$sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "i", $param_ID);

            // Set parameters
            $param_ID = trim($_POST["ID"]);

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Records deleted successfully. Redirect to landing page

                header('Location:index.php');
               exit;


            }else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);

        // Close connection
        mysqli_close($link);
    } else{
        // Check existence of ID parameter
        if(empty(trim($_GET["ID"]))){
            // URL doesn't contain ID parameter. Redirect to error page
            header("location: error.php");
            exit();
        }

    }
   ?>

Any help would be great.

Warning: Cannot modify header information - headers already sent by (output started at /storage/public_html/crudbd/config.php:31) in /storage/public_html/crudbd/delete.php on line 26

  • Have you tried with absolute path? – Cid May 25 '18 at 11:56
  • 1
    Possible duplicate of [PHP header location-redirect doesn't work - why?](https://stackoverflow.com/questions/2710079/php-header-location-redirect-doesnt-work-why) – Ende May 25 '18 at 11:56
  • @Niels he did `if($stmt = mysqli_prepare($link,$sql))` – Cid May 25 '18 at 11:59
  • ini_set('display_errors',1); error_reporting(E_ALL); put these two line top of the page and see the error . – JYoThI May 25 '18 at 11:59
  • 1
    @Niels he is doing assign not comparing – JYoThI May 25 '18 at 12:01
  • JYoThl, No. He should turn display_errors OFF and tail his log. Any notices warnings or errors will force the early sending of headers if that is turned on. – delboy1978uk May 25 '18 at 12:02
  • 1
    @Niels no, there's no `==`. `if ($something = somefunction())` is the same thing than doing `$something = somefunction(); if($something)` – Cid May 25 '18 at 12:02
  • Would this work for you? `echo '';` – Ende May 25 '18 at 12:03
  • First of all we need to know the error then we can know the problem . don't assume @delboy1978uk – JYoThI May 25 '18 at 12:03
  • the error is the redirect isnt happening, he already told us what the expected and actual behaviour was. Checking the log will tell him if he hasn't put sufficient checks in his code etc, but i am telling you right now that HTML output is what is stopping his header call from working. – delboy1978uk May 25 '18 at 12:05
  • Can you try echo something out, make an alert, or whatever obvious message you can make inside your if statement and out-comment the header part? Just to see if you actually enter the statement where the redirect takes place. If you do happen to enter the if statement, and the redirect doesn't happen, do you get any error anywhere, at all? Anything that we can us at all? If you do enter the if statemen, and it doesn't work, try add / infront of index so that it becomes so: header('Location: /index.php'); – Martin May 25 '18 at 13:16
  • Warning: Cannot modify header information - headers already sent by (output started at /storage/public_html/crudbd/config.php:31) in /storage/public_html/crudbd/delete.php on line 26 – guerreiroandre May 25 '18 at 17:46

1 Answers1

-2

As soon as you echo ANYTHING, HTTP headers get sent, thereby making it impossible to send any more, causing your redirect header to FAIL.

Check for echo statements in config.php, and turn display_errors off, error_reporting to -1, and tail the log in the terminal to make sure any error log stuff isnt causing PHP to output early.

UPDATE

You pasted your error. Warning: Cannot modify header information - headers already sent by (output started at /storage/public_html/crudbd/config.php:31) in /storage/public_html/crudbd/delete.php on line 26

So, as I said, I was correct in guessing that there was output in config.php, however it wasn't an echo.

If you turn display_errors off, it should work. The notice or warning will still appear in your log.

Find out what is happening on that line. If there is an undefined variable which you don't first check with isset() for example, that could trigger log output.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39