1

I am trying to pass a variable in a URL within a header function. I know I can do it like this index2.php?ID=<?=$objResult["ID"];?> if its not in a header function. However if I try to do this in the header function my whole script crashes. I have also tried this:

header("Refresh: 5;url=index2.php?ID=".$objResult["ID"]."");

but that gives the following error:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/Mail/index.php:25) in /var/www/Mail/index.php on line 33

(Line 33 is the header line)

My whole code is this:

<?php
if($_POST)
{
    mysql_connect('localhost','root','root');
    mysql_select_db('NAW') or die (mysql_error());

    $Naam              =    $_POST['naam'];
    $Email             =    $_POST['email'];
    $Soort             =    $_POST['soort'];

    $sql = mysql_query("INSERT INTO Klant (Naam, Email, Soort) VALUES ('".$Naam."', '".$Email."', '".$Soort."')") or die (mysql_error());

    if ($sql === false) {

        die (mysql_error());

    }

    else {

        echo ''.$Naam.' is in de database toegevoegd.<br><br>';

    }

    $strSQL         = ("SELECT * FROM Klant WHERE Naam='".$Naam."'");
    $objQuery       = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    $objResult      = mysql_fetch_array($objQuery);  

    header("Refresh: 5;url=index2.php?ID=".$objResult["ID"]."");


    if($sql){ return 1; } else { return 0; }

}
?>

If anyone can tell me what I am doing wrong on push me in the right direction it would be great!

NOTE:

I know I shouldn't be using mysql_* because of sql injections but since this code will never be online and only used local this is not a problem

Passerby
  • 9,715
  • 2
  • 33
  • 50
Daanvn
  • 1,254
  • 6
  • 27
  • 42

1 Answers1

5

You can't send your headers if there's already been output to the page, which you have with:

echo ''.$Naam.' is in de database toegevoegd.<br><br>';

There's a great answer regarding exactly this, here.

Community
  • 1
  • 1
Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • 1
    Thnx, that solved the problem and indeed a nice explanation on that other answer! (I will accept your answer when its possibble) – Daanvn Apr 15 '13 at 09:40