Why is this wrong? Why do I get the darn Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Yank\add_stuff.html.php:16) in C:\xampp\htdocs\Yank\index.php on line 24?
If you have any doubts about what output might have been sent, then try this simple debugging test:
Comment out your header(), and echo something like “Redirecting to xxx.php”
And then see what output you get when executing the script… remember to look at the source code in the browser, to see if any HTML have been outputtet before the “Redirecting to xxx.php”.
If there are anything before the “Redirecting to xxx.php”, you must change your script.
Example of how you could test it:
if (!isset($_POST['djur']))
{
include 'add_stuff.html.php';
exit();
}
include 'connect.php';
$djur = mysqli_real_escape_string($link, $_POST['djur']);
$park = mysqli_real_escape_string($link, $_POST['park']);
$sql = 'INSERT INTO olika_djur SET
djur="' . $_POST['djur'] . '",
park="' . $_POST['park'] . '"';
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted joke: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
//header('Location: .');
echo "Redirecting to .";
exit();
Any other output than “Redirecting to .” means you have to change your script.
Try the above change, comeback here and post the resulting source code from the browser.
If you already send ANY output to the browser, you will get that error message.
In this case, the problem was caused by including the ‘add_stuff.html.php’ file (which outputs all that html code immediately) and then trying to redirect.
Hmm… but does not my use of the exclamation change that?
if (!isset($_POST['djur']))
I mean, if you look at the last post with the code that works, I am still including the add.stuff file after the if statement. There must be something else that’s causing the error message?!