Don't Escape
I'm a werewolf and it's a full moon. I have to find a way to prevent myself from escaping.
3.82 / 5.00 33,622 ViewsRagnarok Online Jigsaw
Did you play Ragnarok Online? Do you like that game?
3.55 / 5.00 13,314 ViewsThis 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> </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! :)
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.
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.
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?
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
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 ]
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...
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 ]
i can allready post info into the table an it shows up in PhpMyAdmin so the table is difined and named corectly...
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 ]
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
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.
I wrote this like the third day I was learning PHP, I believe...I probably screwed up about 90% of it :)
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.
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.
im trying to post the code but the stupid forum keeps giving me errors.
i didnt change any code besides the mysql connection stuff. Everything else is as it is in the HOWTO above.
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.
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;"