-3

I'm aware of the fact that for header(Location) to work, no output must be sent before. The problem is that I've checked my code so many times but can't find what is actually being sent as output, thus preventing my header(Location) from working.

Can anyone spot the error?

    <div id="content">
        <h2>Lägg till</h2>
        <p>Fyll i fälten och klicka på Lägg till för att skapa en ny kontakt i listan.</p>
        <?php
            $editid = $_GET['contact_id'];
            $query = "SELECT *, Persons.p_id FROM Persons INNER JOIN Pictures ON (Pictures.p_id = Persons.p_id) WHERE Persons.p_id = " . $editid;
            $result = mysql_query($query);

            while ($row = mysql_fetch_array($result)) {
                $name = $row['name'];
                $address = $row['address'];
                $birthday = $row['birthday'];
                $picture = $row['source'];
                $p_id = $row['p_id'];

            }
        ?>  

        <form action="" id="addressForm" method="post">
            <ul>
                <li><label for="name"><strong>Namn</strong></label><input type="text" name="name" id="name"/></li>
                <li><label for="address"><strong>Adress</strong></label><input type="text" name="address" id="address"/></li>

                <li><label for="year"><strong>Födelsedag</strong></label>
                <select id="year" name="year">
                    <option value="2013">2013</option>
                    <option value="2012">2012</option>
                    <option value="2011">2011</option>
                    <option value="2010">2010</option>
                    <option value="2009">2009</option>
                    <option value="2008">2008</option>
                    <option value="2007">2007</option>
                    <option value="2006">2006</option>
                    <option value="2005">2005</option>
                    <option value="2004">2004</option>
                    <option value="2003">2003</option>
                    <option value="2002">2002</option>
                    <option value="2001">2001</option>
                    <option value="2000">2000</option>
                    <option value="1999">1999</option>
                    <option value="1998">1998</option>
                    <option value="1997">1997</option>
                    <option value="1996">1996</option>
                    <option value="1995">1995</option>
                    <option value="1994">1994</option>
                    <option value="1993">1993</option>
                    <option value="1992">1992</option>
                    <option value="1991">1991</option>
                    <option value="1990">1990</option>
                </select>
                <select name="month">
                    <option value='1'>1</option>
                    <option value='2'>2</option>
                    <option value='3'>3</option>
                    <option value='4'>4</option>
                    <option value='5'>5</option>
                    <option value='6'>6</option>
                    <option value='7'>7</option>
                    <option value='8'>8</option>
                    <option value='9'>9</option>
                    <option value='10'>10</option>
                    <option value='11'>11</option>
                    <option value='12'>12</option>
                </select>
                <select name="day">
                    <option value='1'>1</option>
                    <option value='2'>2</option>
                    <option value='3'>3</option>
                    <option value='4'>4</option>
                    <option value='5'>5</option>
                    <option value='6'>6</option>
                    <option value='7'>7</option>
                    <option value='8'>8</option>
                    <option value='9'>9</option>
                    <option value='10'>10</option>
                    <option value='11'>11</option>
                    <option value='12'>12</option>
                    <option value='13'>13</option>
                    <option value='14'>14</option>
                    <option value='15'>15</option>
                    <option value='16'>16</option>
                    <option value='17'>17</option>
                    <option value='18'>18</option>
                    <option value='19'>19</option>
                    <option value='20'>20</option>
                    <option value='21'>21</option>
                    <option value='22'>22</option>
                    <option value='23'>23</option>
                    <option value='24'>24</option>
                    <option value='25'>25</option>
                    <option value='26'>26</option>
                    <option value='27'>27</option>
                    <option value='28'>28</option>
                    <option value='29'>29</option>
                    <option value='30'>30</option>
                    <option value='31'>31</option>
                </select>
                </li>
                <li><label for="picture"><strong>Bild (URL)</strong></label><input type="text" name="picture" id="picture"/></li>   
                <li><input type="submit" id="submit" name="submit" value="Lägg till"/></li>
            </ul>
        </form>
        <?php
            if(isset ($_POST['submit']))
            {
                $editname = mysql_real_escape_string(htmlspecialchars($_POST['name']));
                $editaddress = mysql_real_escape_string(htmlspecialchars($_POST['address']));
                $editpicture = mysql_real_escape_string(htmlspecialchars($_POST['picture']));

                $year = $_POST['year'];
                $month = $_POST['month'];
                $day = $_POST['day'];

                if ($month < 10)
                {
                    $month = "0$month";
                }
                if ($day < 10)
                {
                    $day = "0$day";
                }
                $editbirthday = $year . "-" . $month . "-" . $day;

                if (!empty($name) && !empty($address)){
                    $update = "UPDATE Persons SET name = '$editname', address = '$editaddress', birthday = '$editbirthday' WHERE p_id = '$editid'";
                    $result = mysql_query($update);

                    $query = "SELECT * FROM Persons WHERE p_id = '$editid' LIMIT 1";
                    $result = mysql_query($query);
                    while ($row = mysql_fetch_array($result)) {
                        $p_id = $row['p_id'];

                        $update = "UPDATE Pictures SET source = '$editpicture' WHERE p_id = '$editid'";
                        $result = mysql_query($update);
                        header('Location: index.php');
                    }
                }
            }
        ?>
    </div>
Erik
  • 47
  • 1
  • 2
  • 8
  • 1
    Well, heaps of HTML is sent before, or isn't it? – mario Jul 28 '13 at 16:28
  • "Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file." from manual – UnknownError1337 Jul 28 '13 at 16:29
  • @mario I've had no trouble in the past having the form like that. Or what did you mean specifically? – Erik Jul 28 '13 at 16:30
  • Try moving your last PHP code block to the top of your page above your content DIV?? – Malcolm Jul 28 '13 at 16:35
  • @Malcolm I've tried that, but then the variables get empty 'cause the – Erik Jul 28 '13 at 16:39
  • Have you tried putting the php script you have after the form on it's own page then in the action section of the form send it to that page? – puddleJumper Jul 28 '13 at 18:45

4 Answers4

2

Remember that an output could be:

  • Whitespace before <?php or after ?>
  • UTF-8 Byte Order Mark
  • Error messages or notices
  • print, echo
  • Raw <html> areas before <?php code.

So you're hitting the final point of this list...

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
0

You must put the header ABOVE any HTML output. You can just put it at the top of your document in this case. So just put all the PHP at the top.

Put the whole if statement from the following, to the top of the PHP file. I don't think this will cause any issues.

if(isset ($_POST['submit']))

I hope this helps

Marcus Hughes
  • 5,123
  • 1
  • 25
  • 39
  • Thanks, I was thinking of something like that as well, but it seems the – Erik Jul 28 '13 at 16:37
  • That's not true, because $_POST is set the moment the page is requested. You can try with `print_r($_POST);` at the top – Marcus Hughes Jul 28 '13 at 16:39
  • Okay, but if I try `print_r($_POST)` I just outputs `Array ( )`, which is empty, right? – Erik Jul 28 '13 at 16:43
  • Correct, but post the form, then it will have $_POST data – Marcus Hughes Jul 28 '13 at 16:49
  • You are correct. I now found out that when using htmlspecialchars and mysql real escape string, the $_POST fields get empty. Any idea how come? – Erik Jul 28 '13 at 16:54
  • Tested both of these: `echo "TEST: " . mysql_real_escape_string(htmlspecialchars($_POST['name'])); echo "NAMN: " . $_POST['name'];` with the first line printing out empty space and the second one the actual content of name field – Erik Jul 28 '13 at 16:55
  • Oh, it is possible you are not connecting to mysql. Make sure you are connected to mysql before running `mysql_real_escape_string` – Marcus Hughes Jul 28 '13 at 16:57
  • I am connected, otherwise it would die(). So I really have no idea what to do. – Erik Jul 28 '13 at 17:04
  • Maybe it would die AFTER... but you need to `mysql_connect` BEFORE using `mysql_real_escape_string` ... try putting on `error_reporting(E_ALL);` – Marcus Hughes Jul 28 '13 at 19:09
0

You already have output. Headers MUST be set before any output is sent.

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

Refer to the documentation about it. Hope this helps.

MisterBla
  • 2,355
  • 1
  • 18
  • 29
-1
  • no output
  • no whitespace
  • save file without BOM

Encoding - Notepad++

user1756209
  • 573
  • 10
  • 23