iLuke Posted January 23, 2009 Share Posted January 23, 2009 Hey all =] I started learning PHP about 3 days ago, and am trying to progress onto more difficult things. I was following a tutorial for a simple shoutbox, and came up with the following code: <html> <body> <?php require_once("db.php"); $name = $_SESSION['myusername']; $shout = $_POST['shout']; $slength = strlen($shout); $maxlength = 150; if ($_POST['submit']) { if ($shout == "") { echo "You must enter a message!"; } else if ($slength > $maxlength) { echo "Message was too long!"; } else { $db = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); mysql_query("INSERT INTO shouts(User, Post) VALUES('$name', '$shout')"); } } } $db = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname) or die(mysql_error()); $query = "SELECT * FROM shouts ORDER BY ShoutId DESC LIMIT 20"; $result = mysql_query($query); while($r = mysql_fetch_array($result)) { $shout = $r['Post']; $shout = strip_tags($shout); echo "<li><strong>>$name</strong>: $shout</li>\n"; } mysql_close($db); <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <strong>Message:</strong><br/> <textarea name="message"></textarea><br/> <input type="submit" name="submit" value="Shout It!"> </form> ?> </body> </html> This doesn't show anything up when it's uploaded, and when viewing offline it shows the PHP as text in the browser. Because I followed the tutorial, I'm not sure what to look for in terms of mistakes and so I can't see why it wont work. All database details are correct, but it's the PHP I'm finding hard to debug. Could you guys help me out please =] Thanks! Also, if you could post what was wrong that'd be great so that I can learn for the future =] Thanks in advance =] Luke. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/ Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 You are not closing your PHP in the right spot. mysql_close($db); ?> Should fix it. I would, however, remove that mysql_close as it is not necessary. PHP Closes that connection automatically once the script is done. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744376 Share on other sites More sharing options...
iLuke Posted January 23, 2009 Author Share Posted January 23, 2009 Ahh, thanks a lot.. I'm just uploading files now =] <html> <body> <?php require_once("db.php"); $name = $_SESSION['myusername']; $shout = $_POST['shout']; $slength = strlen($shout); $maxlength = 150; if ($_POST['submit']) { if ($shout == "") { echo "You must enter a message!"; } else if ($slength > $maxlength) { echo "Message was too long!"; } else { $db = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); mysql_query("INSERT INTO shouts(User, Post) VALUES('$name', '$shout')"); } } } $db = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname) or die(mysql_error()); $query = "SELECT * FROM shouts ORDER BY ShoutId DESC LIMIT 20"; $result = mysql_query($query); while($r = mysql_fetch_array($result)) { $shout = $r['Post']; $shout = strip_tags($shout); echo "<li><strong>>$name</strong>: $shout</li>\n"; } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <strong>Message:</strong><br/> <textarea name="shout"></textarea><br/> <input type="submit" name="submit" value="Shout It!"> </form> </body> </html> This is what I have now, but it's still not showing anything =[ This is the url: http://www.privatepie.com/shoutbox/ Thanks for helping though! Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744398 Share on other sites More sharing options...
MadTechie Posted January 23, 2009 Share Posted January 23, 2009 Add <?php die("TESTING"); ?> at the very top, to check it is infact a page problem and not a rewrite problem Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744407 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 <?php require_once("db.php"); $name = $_SESSION['myusername']; ?> <html> <body> <?php $shout = $_POST['shout']; $slength = strlen($shout); $maxlength = 150; Your error reporting is turned off, but the issues was you cannot call session_start after output has been sent to the browser. If it is still blank try this: <?php ini_set("display_errors", 1); error_reporting(E_ALL); require_once("db.php"); $name = $_SESSION['myusername']; ?> <html> <body> <?php $shout = $_POST['shout']; $slength = strlen($shout); $maxlength = 150; And see what the error is. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744409 Share on other sites More sharing options...
iLuke Posted January 23, 2009 Author Share Posted January 23, 2009 I tried both your methods (cheers for helping again guys!!) And still nothing... it's looking like a really bad error now right =[ Code as now: <?php die("TESTING"); ini_set("display_errors", 1); error_reporting(E_ALL); require_once("db.php"); $name = $_SESSION['myusername']; ?> <html> <body> <?php $shout = $_POST['shout']; $slength = strlen($shout); $maxlength = 150; if ($_POST['submit']) { if ($shout == "") { echo "You must enter a message!"; } else if ($slength > $maxlength) { echo "Message was too long!"; } else { $db = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); mysql_query("INSERT INTO shouts(User, Post) VALUES('$name', '$shout')"); } } } $db = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname) or die(mysql_error()); $query = "SELECT * FROM shouts ORDER BY ShoutId DESC LIMIT 20"; $result = mysql_query($query); while($r = mysql_fetch_array($result)) { $shout = $r['Post']; $shout = strip_tags($shout); echo "<li><strong>>$name</strong>: $shout</li>\n"; } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <strong>Message:</strong><br/> <textarea name="shout"></textarea><br/> <input type="submit" name="submit" value="Shout It!"> </form> </body> </html> I tried each method separately, but I put them both in the code above to show where I put them to test it out =] Sorry to be a pain lol! Thanks again, Luke. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744424 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 <?php die("TESTING"); ini_set("display_errors", 1); error_reporting(E_ALL); require_once("db.php"); $name = $_SESSION['myusername']; ?> The DIE("TESTING"); will kill the script, remove that line and try it. Are you getting TESTING on that page. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744430 Share on other sites More sharing options...
iLuke Posted January 23, 2009 Author Share Posted January 23, 2009 Nothing showed up with the testing thing in there =[ I took the line out as you said, and it still wont work =[ I have to go to work now, but I think this problem is realll bad heh! Sorry again to be a pain, Thanks, Luke. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744444 Share on other sites More sharing options...
MadTechie Posted January 23, 2009 Share Posted January 23, 2009 Okay it sounds like a rewrite problem.. have to created an .htaccess files ? added anything to "tidy the url" etc ? Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744449 Share on other sites More sharing options...
iLuke Posted January 23, 2009 Author Share Posted January 23, 2009 Well this shoutbox is just stored in a folder called shoutbox within the public_html folder, and the main file is called index.php. I do have some PHP files working, such as a log in system and a registration system. There is a .htaccess file in the public_html folder, but not one in the shoutbox one. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744794 Share on other sites More sharing options...
MadTechie Posted January 23, 2009 Share Posted January 23, 2009 try renaming it to shoutbox.php and put it in the public_html folder then open it.. or rename the .htaccess file to 1.htaccess Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744839 Share on other sites More sharing options...
iLuke Posted January 24, 2009 Author Share Posted January 24, 2009 Thanks mate. I tried that to no avail.. I downloaded the source code from here: http://nettuts.com/php/create-a-basic-shoutbox-with-php-and-sql/ I really really didn't wanna use someone else's source code... even though I've edited it a lot myself and understand most of it - it's better practice for me to write my own... but that was totally busted lol! Anyways, I decided that, since this file works now, that I'd stick with this, and just keep trying to add my own style bit by bit until it works and looks as I'd like. Thank you very very much for your help! I still don't know what was wrong, or why, but at least I have a working shoutbox now.. I'll try and create the next thing all by myself heh! Thanks again, you were very helpful guys! Luke. Quote Link to comment https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/#findComment-744872 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.