Russia Posted January 31, 2011 Share Posted January 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/ Share on other sites More sharing options...
kenrbnsn Posted January 31, 2011 Share Posted January 31, 2011 Did you read the sticky article on this topic before posting? Also, please post the entire error message. Ken Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167595 Share on other sites More sharing options...
Russia Posted January 31, 2011 Author Share Posted January 31, 2011 Okay sorry about that. -- Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\update2.php:74) in C:\wamp\www\update2.php on line 100 -- I forgot to add that. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167603 Share on other sites More sharing options...
JD* Posted January 31, 2011 Share Posted January 31, 2011 You cannot do anything with the header once you echo text out to the page (IE all of your form code) Put all of your if($_POST)... code at the top of your page and you should be fine Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167605 Share on other sites More sharing options...
Russia Posted January 31, 2011 Author Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167607 Share on other sites More sharing options...
JD* Posted January 31, 2011 Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167626 Share on other sites More sharing options...
Russia Posted January 31, 2011 Author Share Posted January 31, 2011 Okay sir, now where would I put the die() again? Like post the line edited with the die() statment inside it... Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167628 Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 Are those 3 separate pages or 1? As a start, either that is NOT the entire code or you have <tr>'s and <td>'s OUTSIDE your table tags. It helps to cleanup obvious errors first, that makes it easier to pindown the less obvious ones. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167630 Share on other sites More sharing options...
Russia Posted January 31, 2011 Author Share Posted January 31, 2011 Yeah thats the entire code. There seems to be no html problems. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167632 Share on other sites More sharing options...
trq Posted January 31, 2011 Share Posted January 31, 2011 The error tells you exactly what line is producing output. output started at C:\wamp\www\update2.php:74 What is on line 74? Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167634 Share on other sites More sharing options...
Russia Posted January 31, 2011 Author Share Posted January 31, 2011 And plus theres no error showing that you just posted... So I have no idea why your getting that message. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167636 Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167640 Share on other sites More sharing options...
suma237 Posted January 31, 2011 Share Posted January 31, 2011 instead of the code header("location:update2.php"); replace it with ?> <script language='javascript'> window.location.href='update2.php'; </script> <?php ?> Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167686 Share on other sites More sharing options...
trq Posted January 31, 2011 Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167688 Share on other sites More sharing options...
btherl Posted January 31, 2011 Share Posted January 31, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/226178-cannot-modify-header-information-headers-already-sent-starting-to-annoy-me/#findComment-1167706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.