-2

I am getting a warning Cannot modify header information- headers already sent by (output started...). What I have done is if the user submitted data, it will be inserted into the database and the page is redirected to the 'members.php'. Here is my code...

include('connect.php');
if(isset($_POST['submit']))
{
    $name= $_POST['name'];
    $contact= $_POST['contact'];
    $college= $_POST['college'];
    $address= $_POST['address'];
    $insert_query= mysql_query("insert into members_details(member_name,contact_no,college,address) values('".$name."','".$contact."','".$college."','".$address."')");
    if($insert_query)
    {
       header("Location: members.php");
    }
}

//members.php code

include('connect.php');

$select_query= mysql_query("select * from members_details order by reg_no");

$rows= mysql_num_rows($select_query);

user152
  • 1
  • 1
  • There may be a output before any header. Please show some code of yours – Khushboo Jun 17 '14 at 05:58
  • @Khushboo: i have added the code in the post. – user152 Jun 17 '14 at 06:13
  • You're outputting content somewhere before this, which is why you're getting the `headers already sent` error. You need to look at the files that reference to this/call this one. – Darren Jun 17 '14 at 06:14
  • 1
    -1 : [**if u could just google this....**](https://www.google.com/search?q=Warning%3A+Cannot+modify+header+information-+headers+already+sent+by&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a#q=Warning%3A+Cannot+modify+header+information-+headers+already+sent+by+stackoverflow&rls=org.mozilla:en-US:official&safe=off) – NoobEditor Jun 17 '14 at 06:20
  • Is there anything before if(isset($_POST['back_button'])) //back_button is also a submit button. { header("Location: members.php"); } – Khushboo Jun 17 '14 at 06:32
  • no, this the only code that is written within php – user152 Jun 17 '14 at 06:53

1 Answers1

0

As posted here:

link

Some functions modifying the HTTP header are:

  • header / header_remove
  • session_start / session_regenerate_id
  • setcookie / setrawcookie

You are getting that error because you are using echo to display your text while the HTTP headers(mentioned above) are already passed through. You're outputting some stuff before this code that you have provided. You need to look at the files or the code that is outputting the header. Please refer to my link for more details.

Community
  • 1
  • 1
  • but i have used if else condition, and the text will be displayed when the 'submit' button is clicked... not the 'back' button... – user152 Jun 17 '14 at 06:24
  • The only way to get around this to play only the echo part before the HTTP headers I have mentioned in the post. You can repeat you if and else statement to just have the echo condition when you are putting it over the HTTP headers –  Jun 17 '14 at 06:27