eric11 Posted October 8, 2011 Share Posted October 8, 2011 Hi, I'm having trouble with redirect my page to another page. what I have is a page that uses a form to insert into a database, the form is then processed to another page (insert.php) which will insert the information inserted in the form. I'm trying to work out how I could redirect the user to another page once the data has been successfully inserted. this is what I have now: insert.php <?php $link = mysql_connect('ericlee.dot5hostingmysql.com', 'user', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } //echo 'Connected successfully'; mysql_select_db('remarch', $link); $sql="INSERT INTO posts (Name, Location, Fault, Other, Description) VALUES ('$_POST[name]','$_POST[location]','$_POST[fault]','$_POST[other]','$_POST[description]')"; header('Location: about.html'); if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } mysql_close($link); ?> I'm pretty sure it's just the placing of the code header('Location: about.html');. Would be appreciated if someone could give me some guidance Thank you in advance! Quote Link to comment Share on other sites More sharing options...
sunfighter Posted October 8, 2011 Share Posted October 8, 2011 You never said what trouble you were having. I ran your script and the only thing I get is 'about.html' not found. Is about.html in the same folder as this program? Does the program write to the screen? The only two thing that might cause the redirect to not work. Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 Move the header function call to after the mysql_close call. This will ensure that it was successful before redirecting. ~juddster Quote Link to comment Share on other sites More sharing options...
vulture Posted October 8, 2011 Share Posted October 8, 2011 I haven't tried the code.. But a die() or exit() after header might help... Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 @vulture - the exit / die isn't required Quote Link to comment Share on other sites More sharing options...
eric11 Posted October 9, 2011 Author Share Posted October 9, 2011 You never said what trouble you were having. I ran your script and the only thing I get is 'about.html' not found. Is about.html in the same folder as this program? Does the program write to the screen? The only two thing that might cause the redirect to not work. sorry, didn't clarify well. the error that comes up is Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web221/b2217/d5.ericlee/public_html/remarch/insert.php:10) in /hermes/bosweb/web221/b2217/d5.ericlee/public_html/remarch/insert.php on line 29 or whatever line the header is at. the 'about.html' page is located in the same folder... i was able to solve almost the same issue in another script/page i have.. but for this page... it's not working.. ><" the program does not write to the screen... simply, once the form is submitted... it will redirect to the about page automatically. any other ideas? thanks guys Quote Link to comment Share on other sites More sharing options...
Buddski Posted October 9, 2011 Share Posted October 9, 2011 That error means that you have output on your screen on line 10. You cannot send ANY information to a brower before a header() call. Also adding exit() after a header() is a good idea as it helps prevent any other script execution. Quote Link to comment Share on other sites More sharing options...
eric11 Posted October 9, 2011 Author Share Posted October 9, 2011 on line 10 its just a <?php i tried putting up the header before that.. however.. similar errors still show up. at the moment i have: <body> <?php header('Location: about.html'); ?> <?php $link = mysql_connect('.com', 'xxx', 'xxx'); if (!$link) { die('Could not connect: ' . mysql_error()); } //echo 'Connected successfully'; mysql_select_db('remarch', $link); $sql="INSERT INTO posts (Name, Location, Fault, Other, Description) VALUES ('$_POST[name]','$_POST[location]','$_POST[fault]','$_POST[other]','$_POST[description]')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } mysql_close($link); ?> </body> Quote Link to comment Share on other sites More sharing options...
Buddski Posted October 9, 2011 Share Posted October 9, 2011 You are sending <body> to the browser.. ALL headers MUST be at the top of ALL output.. Quote Link to comment Share on other sites More sharing options...
eric11 Posted October 9, 2011 Author Share Posted October 9, 2011 ahh cool. thanks for the clarification. thanks all of you! appreciated heaps Quote Link to comment 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.