View Full Version : PHP redirection does not work

03-10-2009, 09:22 AM

I have a form to create a new location.It updates the new record to the DB but it idplays blank page with error message.

"Cannot modify header information - headers already sent by"
Could some help me to correct my code to get rid of the error.

My main aim is to display html_location.php after the new record is added.

$building = $_REQUEST['building'];
$submit = $_REQUEST['submit'];

//Check if form has been submitted
if ($submit==1){
//set variables of from form
$building = $_REQUEST['building'];

//Error check
if (empty($building)){
$message ="The building name is required.<br/>";

//If the are no errors, insert the data to the database.
if (empty($message)){

$con = mysql_connect('test', 'user', 'new');

mysql_select_db("test", $con);
//SQL to insert new building
$query = "INSERT INTO buildings (Name) VALUES (upper('$building'))";
mysql_query($query) or die('Error, insert query failed');
//Return the user to the project's page


<title>Building - New</title>
<link rel="stylesheet" href="styles.css" type="text/css">

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<td align="left" class="head">Location - New Building</td>
<td align="right" valign="bottom" class="text">&nbsp;</td>

<table border="0" cellspacing="0" cellpadding="4" width="100%">
<td width="114" class="activetab">Add New Bulding</td>
<td class="nontab">&nbsp;</td>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<?php if (!empty($message)) {?>

<td class="Text" colspan="2"><?php echo $message;?></td>

<?php } ?>

<form name="building_form" method="post" action="">
<input type="hidden" name="submit" value="1" >
<td align="left" class="text">Building Name</td>
<td align="left" class="text">
<input class="inputfield" name="building" id="build" size="25" ></td>
</tr> <tr><td colspan="2" align="left"
<tr style="height: 100%; vertical-align: bottom;">
<td colspan="2" align="right">
<input type="submit" value="Save" class="form">&nbsp;&nbsp;</td>


03-10-2009, 09:25 AM
Please post the exact error message you get.

03-10-2009, 10:28 AM
Warning: Cannot modify header information - headers already sent by (output started at /usr/share/html_view_newbuilding.php:2) in /usr/share/html_view_newbuilding.php

03-10-2009, 10:51 AM
This usually means that you have printed something to the screen already. Either by using echo or similar.

Is the code you sent the entire php file, or is it included in some other file?

03-10-2009, 11:13 AM
Thats the complete code. i am no printing anything before or using any echo statement.

03-10-2009, 11:14 AM
Looks like the same file.
This is a little unusual though, generally pre-opening output is generated at line 1. I'm thinking its some byte order markings in there, so you may want to consider using a standard notepad / wordpad instead of the IDE you're currently using.
To fix, open the file html_view_newbuilding.php in notepad and save it back using save as ensuring that ANSI is selected as you're encoding type.
Try that, post back you're results.

Oh yes, if you see anything before the <?php, make sure thats removed as well. This includes any whitespace.

03-10-2009, 11:29 AM
That really worked.....

Thanks for all the help

03-10-2009, 11:35 AM
You're welcome.
Try to find a new editor to use. Any basic editor will work (notepad, vim, phpedit, etc), or IDE specifically developed to handle PHP to get around future troubles with the BOM.