Page 1 of 1

Repeated entries into MySQL. How do I stop it?

Posted: Thu Nov 21, 2002 6:13 am
by Jim
http://www.empiregaming.net/iamjim/news ... P/news.php

Enter a piece of news there and hit the refresh button. Hit refresh as often as you want to! News will just keep popping up and up and up and up.

This is the code for the entire page:

Code: Select all

<?

include("config.php");

if($_POSTї'submit']) {
//Create News
$sql = "insert into IamJim_news (headline, news, posted_by) values('$headline' , '$news' , '$user')";
$query = mysql_query($sql);
}

//Print the News
$sql = "select * from IamJim_news where id != 0";
$query = mysql_query($sql);
	if($query) {
	while($data = mysql_fetch_assoc($query)) {
	
		$headline = $dataї'headline'];
		$date = $dataї'postdate'];
		$news = $dataї'news'];
		$poster = $dataї'posted_by'];
		$id = $dataї'id'];
		
	echo "<table cellpadding=5 cellspacing=0 border=0><tr><td>$headline</td><td>$date</td><td>$poster</td><td width=50><a href="http://www.empiregaming.net/iamjim/news/MySQL-PHP/editnews.php?action=edit&id=$id">Edit</a></td><td width=50><a href="http://www.empiregaming.net/iamjim/news/MySQL-PHP/editnews.php?action=delete&id=$id">Delete</a></td></tr></table>";

	}
	}
	

?>
<form method=post action="<?=$PHP_SELF?>"><table cellpadding=0 cellspacing=0 border=0><tr><td><input type="text" name="user" value="Username"></td></tr><tr><td><input type="text" name="headline" value="Headline"></td>
</tr><tr><td><textarea name="news" cols=20 rows=5>Post News Here</textarea></td></tr><tr><td><input type="submit" name="submit"></table></form>
I figure the problem is in the if($_POST['submit'] { area... but I don't know what the problem is.

Any ideas on how to stop this constant insertion?

Thanks for help!

Posted: Thu Nov 21, 2002 7:29 am
by BDKR
It's definitely that submit button.

One idea would be to redirect the user someplace if the submission is succesful. Someplace he or she is most likely to go anyways.

The other, and maybe the two could be used in conjunction, would be to create a key in the table that submissions are entered into that is the md5 hash of all the data that is going to be entered in. That way, if there is an attempt to resubmit the same data, it will fail as the same hash allready exists in the table.

Hope that was clear.

Cheers,
BDKR

simple, yet effective solution

Posted: Thu Nov 28, 2002 10:01 am
by Intrigue
simply add 'exit(header("Location: " . $PHP_SELF)); after you put your form data into database, and they'll go back to the page, after the news is put into the database :)