Jump to content

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/142122-solved-shoutbox-totally-broken/
Share on other sites

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!

 

 

<?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.

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.

 

<?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.

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.

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.

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.