#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Berlin
    Posts
    4
    Rep Power
    0

    Refreshing Browser keeps inserting values into DB


    Hello Everybody!

    I'm trying to create a DB driven website that dispalys the list of submitted jokes. I'm using a multipurpose-page that show a form whenever a user want to add a joke.
    Once the form field is filled, users click on the submit button which links to the same page, that brings up the list of all submitted jokes.

    Sofar everything works fine, except the Browser Refresh (F5) problem. Once a joke text was submited and inserted into DB, and the user hits the Browser's REFRESH button, the script keeps on inserting same records in my DB again and again ???


    Can You help me change my script so it's able to recognize whether the FORM values has already been inserted into the DB ? Is it still possible to do it with one PHP multi-purpose page?

    Furthermore, do You know how can modify my script, so it would be possible to check whether the JokeText record already exists in the DB - to avoid doubled records. (preventing identical JokeText RECORDS more than once in the DB)

    Thanks in advance for Your help!
    Cheers, Tom

    Here's the code:
    PHP Code:
    <?php
      
    if (isset($_GET['addjoke'])): // If the user wants to add a joke
    ?>

    <form action="<?=$PHP_SELF?>" method="post">
    <p>Type your joke here:<br />
    <textarea name="joketext" rows="10" cols="40" wrap>
    </textarea><br />
    <input type="submit" name="submitjoke" value="SUBMIT" />
    </p>
    </form>
    <?php
      
    else: // Default page display

        // Connect to the database server
        
    $dbcnx = @mysql_connect('localhost''root''');
        if (!
    $dbcnx) {
          die( 
    '<p>Unable to connect to the ' .
               
    'database server at this time.</p>' );
        }

        
    // Select the jokes database
        
    if (! @mysql_select_db('jokes') ) {
          die( 
    '<p>Unable to locate the joke ' .
               
    'database at this time.</p>' );
        }

        
    // If a joke has been submitted,
        // add it to the database.
        
    if (isset($_POST['submitjoke'])) {
          
    $joketext $_POST['joketext'];
          
    $sql "INSERT INTO Jokes SET
                  JokeText='
    $joketext',
                  JokeDate=CURDATE()"
    ;
          if (@
    mysql_query($sql)) {
            echo(
    '<p>Your joke has been added.</p>'); 
          } else {
            echo(
    '<p>Error adding submitted joke: ' .
                 
    mysql_error() . '</p>');
          }
        }
        
        
    // If a joke has been deleted,
        // remove it from the database.
        
    if (isset($_GET['deletejoke'])) {
          
    $jokeid $_GET['deletejoke'];
          
    $sql "DELETE FROM Jokes
                  WHERE ID=
    $jokeid";
          if (@
    mysql_query($sql)) {
            echo(
    '<p>The joke has been deleted.</p>');
          } else {
            echo(
    '<p>Error deleting joke: ' .
                 
    mysql_error() . '</p>');
          }
        }
              
        echo(
    '<p> Here are all the jokes in our database: </p>');

        
    // Request the ID and text of all the jokes
        
    $result = @mysql_query('SELECT ID, JokeText FROM Jokes');
        if (!
    $result) {
          die(
    '<p>Error performing query: ' .
              
    mysql_error() . '</p>');
        }

        
    // Display the text of each joke in a paragraph
        // with a "Delete this Joke" link next to each.
        
    while ( $row mysql_fetch_array($result) ) {
          
    $jokeid $row['ID'];
          
    $joketext $row['JokeText'];
          echo(
    '<p>' $joketext .
               
    '<a href="' $_SERVER['PHP_SELF'] .
               
    '?deletejoke=' $jokeid '">' .
               
    'Delete this Joke</a></p>');
        }

        
    // When clicked, this link will load this page
        // with the joke submission form displayed.
        
    echo("<p><a href='$PHP_SELF?addjoke=1'>"."Add a Joke!</a></p>");

      endif;

    ?>
  2. #2
  3. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    51
    You need to redirect them to $_SERVER['PHP_SELF'] after an insert has been made so that the POST data is lost.
    PHP Code:
    header("Location:".$_SERVER['PHP_SELF']); 

IMN logo majestic logo threadwatch logo seochat tools logo