0

I would like to ask for some help with the following please. The error I am getting is:

Warning: Cannot modify header information - headers already sent by

This is the code I am using:

<?php
    if (isset($_GET['success']) === true && empty($_GET['success']) === true) 
    {
        echo 'Your details have been updated!';
    } 
    else 
    {
        if (empty($_POST) === false && empty($errors) === true) 
        {               
            $newevent = array(
                'title'         => $_POST['title'],
                'discription'   => $_POST['discription'],
                'event_date'    => $_POST['event_date'],
                'time'          => $_POST['time'],
                'contact_name'  => $_POST['contact_name'],
                'contact_num'   => $_POST['contact_num'],
                'username'      => $_POST['username']
            );

            new_event($session_user_id, $newevent);
            header('Location: settings.php?success');
            exit();

        } else if (empty($errors) === false) {
            echo output_errors($errors);
        }
    }
?>

and this is the new_event function:

function new_event($user_id, $newevent) {
    $event = array();
    array_walk($newevent, 'array_sanitize');

    foreach($newevent as $field=>$data) {
        $event[] = '`' . $field . '` = \'' . $data . '\'';
    }

    mysql_query("UPDATE `event` SET " . implode(', ', $event) . " WHERE `username` = $user_id");
}

Any assistance would be much appreciated, I've been looking over it for ages.

Many thanks

Coder Panda
  • 632
  • 3
  • 9
  • 31
  • do you have some html content on the top of the page ? or even white spaces ? more over the query `WHERE `username` = $user_id` should be `WHERE `username` = '$user_id'` assuming `username` is string. – Abhik Chakraborty May 04 '14 at 09:56
  • 4
    I see a space before the php open tag ( – Peter May 04 '14 at 09:57

2 Answers2

0

Use this on top of your page

<?php
ob_start("ob_gzhandler");
?>
Ansh
  • 94
  • 4
0
  1. If possible, try avoiding header if it is just used to trigger other server requests. It produces more waiting time and ping-pong, disturbs browser.back etc. Try finding a way to execute settings.php?success without another HTTP.

  2. header() only works if no single simple character has been output before. You can start ob_start at beginning of your script and do ob_end_clean just before every intended output, and especially before header().

flaschenpost
  • 2,205
  • 1
  • 14
  • 29