-1

I have a mysqli query and if the query is sucessfully run, I want to go to the url, else it is redirected to a different URL.

The problem is that the header loacation is not working and i cannot see why not.

Here is the code :

<!-- ADD -->

<?php

if(isset($_POST['add_category']))
{

// Set a variable to each form input name 
$category = $conn->real_escape_string($_POST['category']);
// Pass variables to database table 


    if (mysqli_query($conn, "INSERT INTO categories(category_name) VALUES('$category')")) {
        header("Location: admin.php?menu=1&alert=1");

    } else {
        header("Location: admin.php?menu=1&alert=2");
    }
}

?>

<!-- DEL -->
<?php

if(isset($_GET['del']))
{    
$id = $_GET['del'];

if (mysqli_query($conn, "DELETE FROM categories WHERE id='$id'")) {

    header("Location: admin.php?menu=1&alert=3");

}

}
?>

<!-- UPDATE -->

I have tried replaceing the first header location with just a echo string and this outputs showing the query runs correctly, the issue must be with the header location. Thankyou for any help

Bradley
  • 129
  • 10

1 Answers1

0

It's the whitespace outside the <?php and ?> tags.

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

Remove whitespace before and put a die() after the header() call

Jaime
  • 1,402
  • 7
  • 15