Duplicate Data

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
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

Duplicate Data

Post by zed420 »

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.

Code: Select all

<?
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>&nbsp;</td>
    <td><textarea name="news" cols="55" rows="15">&nbsp;</textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</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
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

Re: Duplicate Data

Post by yoji »

first off, you need to place the column names..
$query = "INSERT INTO news ('THE_COLUMN1,THE_COLUMN2') VALUES

THE MAIN CULPRIT:
$result = mysql_query($query)or die(mysql_error());

the above won't work... This is causing your double queries...
Post Reply