Duplicate Data

Discussion in 'PHP' started by zed420, Nov 18, 2008.

  1. #1
    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.:confused:

    <?
    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();
    		}
    Code (markup):
    Thanks
    Zed
     
    zed420, Nov 18, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    This is because the submitted form data is still there after being submitted. To avoid this, simply refresh the page ( using php or javascript)

    After this line : echo '<p>Your Fresh news have been added. </p>';

    add this:

    
    if ( mysql_affected_rows() == 1) { // 1 row has been inserted
    
    // redirect the user to another page / to the same page ( to clear submitted data )
    header('location : where_u_want_user_to_go_after_submitted.php');
    
    }
    
    PHP:
    You can do the same using javascript ( put after echo '<p>Your Fresh news have been added. </p>'; )
    
    <script type="text/javascript">
    window.location = window.location
    </script>
    
    HTML:
    ==================

    If you want to display a 'successful' message after the redirect, you can use this:

    
    if ( mysql_affected_rows() == 1) { // 1 row has been inserted
    
    // redirect the user to another page / to the same page ( to clear submitted data )
    header('location : where_u_want_user_to_go_after_submitted.php&success');
    
    }
    
    PHP:
    Then use this at the place you want the success message to be shown
    
    if ( isset($_GET['success']) ) { 
    print 'Success! ';
    }
    
    PHP:
     
    ads2help, Nov 18, 2008 IP
  3. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks ads2help that was a greate help much appreciated

    Zed
     
    zed420, Nov 18, 2008 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    Or try using mysql_insert_id(), b.t.w you need to check your input (validate it) and use mysql_real_escape_string to gain more security! :)
     
    EricBruggema, Nov 19, 2008 IP