Repeated entries into MySQL. How do I stop it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Repeated entries into MySQL. How do I stop it?

Post 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!
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Intrigue
Forum Newbie
Posts: 2
Joined: Tue Nov 19, 2002 9:32 pm
Location: Ohio
Contact:

simple, yet effective solution

Post 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 :)
Post Reply