Web Hosting Talk







View Full Version : PHP/MySQL Shoutbox... issue


Falco1199
09-05-2003, 05:22 PM
I'm making a small shoutbox for my staff control panel, just for the hell of it. Anyway, it works pretty well (and any of you are free to use it), but there's one glitch. If you make a post, and then reload the page, the post is put in the database again, at least in IE where the post variables are still active after a reload. Is there any way I can fix this without making it more than one page? I tried matching the body text, but it didn't seem to work.

Here's the code... Note that it's part of a larger code, so any variable that doesn't get set in it, like $name, is set before this.

$body = $_POST['body'];
if($body == "") print("<center><b>You can't make a shout with nothing in it.</b></center><br />");
print('
<div align="center">
<form method="post" action="'.$_SERVER['PHP_SELF'].'?a=shoutbox" style="margin: 0px; padding: 0px;">
<textarea name="body" cols="60" rows="5" maxlength="1000"></textarea><br />
<input type="submit" value="Add Shout" />
</form>
NOTE: No HTML or BBcode will show up... sorry folks. Just keep it simple and quaker.</div>
<br /><br />
');
if($body && $body != "")
{
$body = htmlspecialchars($body);
$body = addslashes($body);
$query = "
INSERT INTO staff_shoutbox (writer, date, body)
VALUES ('".$name."', NOW(), '".$body."')
";
mysql_query($query) OR sql_error_die();
}
$query = "SELECT * FROM staff_shoutbox ORDER BY ID DESC LIMIT 20";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$date = substr($row['date'],0,8);
$year = substr($date,0,4);
$month = substr($date,4,2);
$day = substr($date,6,2);
if(substr($month,0,1) == "0") $month = substr($month,1,1);
if(substr($day,0,1) == "0") $day = substr($day,1,1);
print('
<hr /><br /><table style="width: 570px; margin-bottom: 14px;" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top" class="bodyText" style="margin: 0px; padding: 6px; padding-top: 3px; padding-bottom: 4px;">
Posted by <b>'.$row['writer'].'</b> on '.$month."-".$day."-".$year.'<br />
&nbsp;&nbsp;&nbsp;'. stripslashes(nl2br($row['body'])).'
</td>
</tr>
</table>
');
} print("<br />");

:cool:

Penguin
09-05-2003, 07:08 PM
As you said, if you reload the page the post method vars will be posted again.

You could send the post vars to another file (i.e. post.php) which adds the shout to the DB, and then does a header("Location: index.php");. (or maybe do that _without_ another file?)

Can't think of anything else right now 8)

Burhan
09-06-2003, 12:56 AM
Just set a cookie when information is posted, with something unique that identifies that post itself.

On a page refresh, the cookie will be available, and you can check to see if its a duplicate post.

If you had a unique key in your database, you could check that also, or you could check for duplicates via SQL.