Jump to content

duplicate data


zed420

Recommended Posts

Hi All

Can someone please point out to me what is wrong with this code? Each time I refresh the page it inserts duplicate data in database. Other than that it doesn't give out any errors. Some help will be greatly appreciated.

 

<?
include("config.php");

function insert(){

$id = $_POST['id'];
$news = $_POST['news'];

   if(empty($news)) error_message("You need to enter some text in news box!");

$query = "INSERT INTO news  VALUES
(NULL,'$news')";
   $result = mysql_query($query)or die(mysql_error());
	if (@mysql_query($query)) {
		echo '<p>Your Fresh news have been added. </p>';
		} else {
		echo '<p>Error adding submitted Information: ' .
		mysql_error() . '</p>';
	}
}

function form(){
?>
<center><h1>Add News</h1>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table width="60%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="18%"></td>
    <td width="82%"><input name="f_id" type="hidden" value="" /></td>
  </tr>
  <tr>
    <td></td>
    <td>Add fresh news here and click 'Submit' button</td>
  </tr>
  <tr>
    <td> </td>
    <td><textarea name="news" cols="55" rows="15"> </textarea></td>
  </tr>
  <tr>
    <td> </td>
    <td><input name="submit" type="submit" value="Submit" />
    <input name="reset" type="reset" value="Reset" /></td>
  </tr>
</table>
</form></center>
<?
}
if (! isset($_POST['submit'])){
	form();
	}else{
	insert();
	}

Thanks

Zed

Link to comment
https://forums.phpfreaks.com/topic/133179-duplicate-data/
Share on other sites

Because it still has the post data and you're running all the functions again. Instead of a success message just redirect.

 

<?
include("config.php");

function insert(){

$id = $_POST['id'];
$news = $_POST['news'];

   if(empty($news)) error_message("You need to enter some text in news box!");

$query = "INSERT INTO news  VALUES
(NULL,'$news')";
   $result = mysql_query($query)or die(mysql_error());
      if (@mysql_query($query)) {
         header("Location: {$_SERVER['REQUEST_URI']}"); exit;
         } else {
         echo '<p>Error adding submitted Information: ' .
         mysql_error() . '</p>';
      }
}

function form(){
?>
<center><h1>Add News</h1>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table width="60%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="18%"></td>
    <td width="82%"><input name="f_id" type="hidden" value="" /></td>
  </tr>
  <tr>
    <td></td>
    <td>Add fresh news here and click 'Submit' button</td>
  </tr>
  <tr>
    <td> </td>
    <td><textarea name="news" cols="55" rows="15"> </textarea></td>
  </tr>
  <tr>
    <td> </td>
    <td><input name="submit" type="submit" value="Submit" />
    <input name="reset" type="reset" value="Reset" /></td>
  </tr>
</table>
</form></center>
<?
}
if (! isset($_POST['submit'])){
      form();
      }else{
      insert();
      }

Link to comment
https://forums.phpfreaks.com/topic/133179-duplicate-data/#findComment-692647
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.