Jump to content

Cannot modify header information - headers already sent STARTING TO ANNOY ME!!!


Russia

Recommended Posts

Hey guys I am getting a Headers error that I havent been able to get rid of for the past 3 hours.

 

I would really appreciate some help. :)

 

 

 


<?php
mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>


    




<form name="form1" method="post" action="">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>Row</strong></td>
<td align="center"><strong>Month - Date</strong></td>
<td align="center"><strong>Message</strong></td>
<td align="center"><strong>Title</strong></td>
<td align="center"><strong>Icon</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">
                   <input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>">
</td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"></td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]"  size="70"  type="text" id="title" value="<?php echo $rows['title']; ?>"></td>
<td align="center">
    <select name="test[]" style="display:block; width:200px" class="mydds">
      <option value="icon_phone.gif" title="icon/icon_phone.gif">Phone</option>
      <option value="icon_sales.gif" title="icon/icon_sales.gif">Graph</option>
    </select> 
</td>
</tr>

<?php
}
?>
<tr>
<td colspan="4" align="center"><br><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>

<?php
if (isset($_POST['Submit'])) { 
for($i=0;$i<$count;$i++){
$month = $_POST['month'];
$date = $_POST['date'];
$message = $_POST['message'];
$title = $_POST['title'];
$monthday = $month[$i]."<br>".$date[$i];
$sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "' WHERE id='$id[$i]'";
$result1 = mysql_query($sql1);
} 
header("location:update2.php");
}
?>

 

Please :)

Link to comment
Share on other sites

I did that but it didnt work.

 

You mean like this?

 

<?php
mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");
$tbl_name="test_mysql";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
// MOVED this code to the top before you output anything
if (isset($_POST['Submit'])) {
for($i=0;$i<$count;$i++){
$month = $_POST['month'];
$date = $_POST['date'];
$message = $_POST['message'];
$title = $_POST['title'];
$monthday = $month[$i]."<br>".$date[$i];
$sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "' WHERE id='$id[$i]'";
$result1 = mysql_query($sql1);
}
header("location:update2.php");
}
?>




<form name="form1" method="post" action="">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>Row</strong></td>
<td align="center"><strong>Month - Date</strong></td>
<td align="center"><strong>Message</strong></td>
<td align="center"><strong>Title</strong></td>
<td align="center"><strong>Icon</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">
<input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>">
</td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"></td>
<td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>"></td>
<td align="center">
<select name="test[]" style="display:block; width:200px" class="mydds">
<option value="icon_phone.gif" title="icon/icon_phone.gif">Phone</option>
<option value="icon_sales.gif" title="icon/icon_sales.gif">Graph</option>
</select>
</td>
</tr>

<?php
}
?>
<tr>
<td colspan="4" align="center"><br><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form> 

 

That got rid of the error message, but now it doesnt update the database like its supposed too.

Link to comment
Share on other sites

Ok, so your next step(s) is to put in a die() ( like die("Got Here"); ) statement in side your post block and make sure that your code is being executed properly. If everything is looking good, echo out your sql query and make sure that it's set up properly:

 

die($sql1);

 

The die statement prevents the rest of the page from finishing and helps you track down what's going on.

Link to comment
Share on other sites

where you have this...

$sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "' WHERE id='$id[$i]'";
$result1 = mysql_query($sql1);

put this...

$sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "' WHERE id='$id[$i]'";
echo $sql1;
exit();
$result1 = mysql_query($sql1);

see what it outputs.

Link to comment
Share on other sites

instead of the code header("location:update2.php"); replace it with

?>

<script language='javascript'>

window.location.href='update2.php';

</script>

<?php

 

?>

 

That is a hack. Organizing your code properly in the first place is always going to be the better option.

Link to comment
Share on other sites

I would find your code easier to read if you indented like this:

 

if (isset($_POST['Submit'])) {
  $month = $_POST['month'];
  $date = $_POST['date'];
  $message = $_POST['message'];
  $title = $_POST['title'];
  for($i=0;$i<$count;$i++){
    $monthday = $month[$i]."<br>".$date[$i];
    $sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "' WHERE id='$id[$i]'";
    $result1 = mysql_query($sql1);
  }
  header("location:update2.php");
}

 

I also moved the assignment from $_POST outside the loop.

 

As for finding your bug, litebearer is giving you the right advice.  The first step in diagnosing a "why doesn't it update the database?" is to display the query, and see if it looks right.  The code he posted:

 

echo $sql1;
exit();

 

is equivalent to this, suggested by JD:

 

die($sql1);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.