Php: Shoutbox (mysql)

  • 4,614 Views
  • 18 Replies
New Topic Respond to this Topic
WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Supporter
Level 14
Programmer
Php: Shoutbox (mysql) Jan. 15th, 2006 @ 11:56 PM Reply

PHP: Main

This tutorial will teach you how to build a shoutbox using MySQL and PHP. This is a pretty simple script, so I'll be classifying this as beginner. Anything in italics is what you will be putting into your code, and normal text is me just talkin' about it. I put it together by taking elements from a few different tutorials I've found.

Let's start with the MySQL database and table. Create a database...I'll be using the example name of shout_box. Insert this as an SQL query through phpMyAdmin (or whatever program is being used)

CREATE TABLE `shoutbox` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`message` TEXT NOT NULL,
`author` VARCHAR(80) NOT NULL,
`date` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`))

Now, create a PHP file. I'll be naming mine shoutbox.php

shoutbox.php
<?php

To start out, connect with the database.

mysql_connect(localhost,shout_user,passwor
d);
mysql_select_db(shout_box);

Alright, we're going to use the same file to display, post, and process shouts, so to start out, we'll do the processing. Let's tell the script to die if no message or username is entered.

if($_POST['submit']) {
if(!$_POST['author']) {
echo 'No name entered.';
die();
}
}
if(!$_POST['message']) {
echo 'No message entered.';
die();
}

Now, let's strip all unwanted HTML, to make sure your page doesn't get messed up.

$message = strip_tags($_POST['message'], '');
$author = strip_tags($_POST['author'], '');

Here is a checker for message and author length. I'm going to use 125 for the message and 30 for the name.

$message_length = strlen($message);
$author_length = strlen($author);
if($message_length > 125) {
echo "Message cannot be longer than 125 characters.";
die();
}
if($author_length > 30) {
echo "Name cannot be longer than 30 characters.";
die();
}

Set the date syntax with the date() function.

$date = date("M t, g:i A");

Now, to finish the posting stuff, let's insert it into the database and close the connection.

$query = "INSERT INTO shoutbox (message, author, date)
VALUES ('$message','$author','$date')";
mysql_query($query);
mysql_close();

Show the user they've posted and send them back to the shoutbox.

echo "Thanks for your post<BR>";
echo "
<A HREF=\"shoutbox.php\">View the shoutbox</a>";

If the user hasn't posted, we want to show them the shoutbox.

} else {
$query = "SELECT message, author, date,
FROM shoutbox order by id DESC LIMIT 10";
$result = mysql_query($query);
echo "<TABLE>";
while($r=mysql_fetch_array($result))
{

Here's where you edit the HTML for the appearance of the posts.

<TR>
<TD>$r[message]</TD>
</TR>
<TR>
<TD><HR></TD>
</TR>";
}
echo "</TABLE>";
?>

At the end of all the shouts, we'll want to put up the posting form.

<FORM METHOD=POST ACTION="shoutbox.php">
<TABLE>
<TR>
<TD>Name :</TD>
<TD><INPUT TYPE="text" NAME="author" SIZE="20"></TD>
</TR>
<TR>
<TD>Message :</TD>
<TD><INPUT TYPE="text" NAME="message" SIZE="20"></TD>
</TR>
<TR>
<TD>&nbsp;</TD>
<TD><INPUT TYPE="submit" name="submit" value="Shout!"></TD>
</TR>
</TABLE>
</FORM>

Close the else statement.

<?php
}
?>

And there ya go! Sure hope that displays right.

An easy way to use this inside another page is the include() function. Just a hint! :)

lefteh
lefteh
  • Member since: Aug. 14, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Php: Shoutbox (mysql) Jan. 16th, 2006 @ 12:05 AM Reply

Nice tut. I've had browser compatibility trouble with if ($_POST['submit']) though. if (isset($_POST['submit'])) seems to have more compatibility. Nice work though.

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Shoutbox (mysql) Jan. 16th, 2006 @ 12:18 AM Reply

At 1/16/06 12:05 AM, lefteh wrote: Nice tut. I've had browser compatibility trouble with if ($_POST['submit']) though. if (isset($_POST['submit'])) seems to have more compatibility. Nice work though.

I'd say go with the if ( isset($var) ) approach. Say you're getting a poll (which is an abstract way of thinking in this situation) and you ask "How many times have you been raped by a tiger?". Well, any NORMAL person would input 0.

No problem, right? Wrong. Checking if ($var) checks the variable for a TRUE value, or any non zero/null value. If they input 0 it'll return false and most likely give them an error.

However, if you use isset() it'll return true because the variable is set, and the value has no relevance to the check.

But still if the value turns up as NULL or any NULL value then the isset() function will also return false, so you don't really win there by picking one method over the other. But what input will consist of a NULL value that is valid anyway, right? =P

Test some of these things out before you post suggest methods as I would do.

Rabbi-Indie-The-Rad
Rabbi-Indie-The-Rad
  • Member since: Dec. 4, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Php: Shoutbox (mysql) Feb. 5th, 2006 @ 12:25 PM Reply

Excellent tut, but my web package doesn't come with phpmyadmin so I was wondering if you could add in the script to do the sql parts entirely in php please?

glomph
glomph
  • Member since: Jun. 16, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 12:48 PM Reply

im atempting to implement this and this warning is coming up Anyone help?

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\index.php on line 106


I have done the deed. Didst thou not hear a noise?

BBS Signature
elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 01:07 PM Reply

Replace every occurence of
mysql_query($query);
with
mysql_query($query) or die(mysql_error());

and report back on the error it gives =)


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
glomph
glomph
  • Member since: Jun. 16, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 01:12 PM Reply

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM shoutbox order by id DESC LIMIT 10' at line 2
i am past the relms of my understanding...


I have done the deed. Didst thou not hear a noise?

BBS Signature
elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 01:14 PM Reply

Are you sure you created the table?
I can't see anything wrong there...


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
glomph
glomph
  • Member since: Jun. 16, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 01:22 PM Reply

i can allready post info into the table an it shows up in PhpMyAdmin so the table is difined and named corectly...


I have done the deed. Didst thou not hear a noise?

BBS Signature
elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 01:24 PM Reply

OK...
Replace
$query = "SELECT message, author, date,
FROM shoutbox order by id DESC LIMIT 10";
with
$query = "SELECT `message`, `author`, `date`,
FROM `shoutbox` ORDER BY `id` DESC LIMIT 10";


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
glomph
glomph
  • Member since: Jun. 16, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 14th, 2006 @ 03:32 PM Reply

At 1/15/06 11:56 PM, WoogieNoogie wrote: $query = "SELECT message, author, date,
FROM shoutbox order by id DESC LIMIT 10";

this had an error in i dont know where but i changed to
SELECT *FROM `shoutbox` WHERE 1
andgot it working fine
thanks for all your help


I have done the deed. Didst thou not hear a noise?

BBS Signature
henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Shoutbox (mysql) Oct. 15th, 2006 @ 06:52 AM Reply

He fucked up the field list by not properly ending it. He put a coma too much, mysql will want another field after a comma, no field and it says you suck.
And have you heard of unsigned numbers? simple slap on the keyword unsigned and the max number size doubles at the cost of not being able to use negative numbers.
And was it just me, or did he forget to output the rest of the fields he quried from the database?


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Supporter
Level 14
Programmer
Response to Php: Shoutbox (mysql) Oct. 17th, 2006 @ 01:48 AM Reply

I wrote this like the third day I was learning PHP, I believe...I probably screwed up about 90% of it :)

rh-penguin
rh-penguin
  • Member since: Feb. 22, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Php: Shoutbox (mysql) Feb. 22nd, 2007 @ 06:32 PM Reply

hi,

When i try and access the shoutbox i get this error.
[code]Parse error: syntax error, unexpected '}' in D:\Apache2\htdocs\shoutbox\shoutbox.php on line 56[/code]On the line 56 there is: } else {
Can someone please tell me whats wrong?
Thanks.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Shoutbox (mysql) Feb. 22nd, 2007 @ 06:35 PM Reply

At 2/22/07 06:32 PM, rh-penguin wrote: hi,

When i try and access the shoutbox i get this error.
[code]Parse error: syntax error, unexpected '}' in D:\Apache2\htdocs\shoutbox\shoutbox.php on line 56[/code]On the line 56 there is: } else {
Can someone please tell me whats wrong?
Thanks.

Post some more of the script. Like 10 lines above that and 10 lines bellow.


BBS Signature
rh-penguin
rh-penguin
  • Member since: Feb. 22, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Php: Shoutbox (mysql) Feb. 22nd, 2007 @ 07:19 PM Reply

im trying to post the code but the stupid forum keeps giving me errors.

rh-penguin
rh-penguin
  • Member since: Feb. 22, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Php: Shoutbox (mysql) Feb. 22nd, 2007 @ 07:21 PM Reply

i didnt change any code besides the mysql connection stuff. Everything else is as it is in the HOWTO above.

humbug88
humbug88
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Php: Shoutbox (mysql) Mar. 12th, 2007 @ 08:59 AM Reply

At 2/22/07 06:32 PM, rh-penguin wrote: hi,

When i try and access the shoutbox i get this error.
[code]Parse error: syntax error, unexpected '}' in D:\Apache2\htdocs\shoutbox\shoutbox.php on line 56[/code]On the line 56 there is: } else {
Can someone please tell me whats wrong?
Thanks.

I think your problem is the extry "}" that was snuck in the HOWTO. Were you see the two "}'s" in a row ner the start, take one out and it may work.

I am also having trouble getting it to work. I get the same "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM shoutbox order by id DESC LIMIT 10' at line 2" error. seeing as i know nothing about mysql i'm totlay stuck. Help anyone?

Thanks.


BBS Signature
humbug88
humbug88
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Php: Shoutbox (mysql) Mar. 13th, 2007 @ 08:30 AM Reply

I fixed the problem after reading through some SQL documentation.

The query should be:
"SELECT message,author,date
FROM shoutbox
ORDER BY id DESC
LIMIT 10;"


BBS Signature