SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    When you go to http://www.webdevelopershq.com/text2.php3 and add a comment on the next page where it says our joke was added if you reload that page it adds the joke into the database again and again for everytime you reload the page. Is there anyway to stop this?


    Code:
    <?php
    // If the user wants to add text
    if (isset($addjoke)):
    ?>
    <FORM ACTION="<?php echo($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"></FORM>
    
    
    <?php
    else:
    //connection variables
    $dbconnect = mysql_connect("localhost", "user", "password");
    
    
    //Cannot Connect
    if (!$dbconnect) {
    echo ("<p>Unable to connect to the database.</p>");
    exit();
    }
    
    
    //select database
    mysql_select_db("jokes", $dbconnect);
    
    //Cannot find DB
    if(! @mysql_select_db("jokes") ) {
    echo ("<p>Unable to locate the database at this time.</p>");
    exit();
    }
    
    echo("<font size='5'><b>Current Comments:</b></font><p>");
    //get TheText from test
    $result = mysql_query("SELECT Joketext FROM Jokes");
    
    //unable to get TheText
    if(!$result) {
    echo ("<p>Unable to process database query</p>");
    exit();
    }
    
    //show the text
    while ($row = mysql_fetch_array($result)) {
    echo("<table width='600' cellspacing='0' border='1' cellpadding='3' bordercolor='#666666' bgcolor='#DDDDDD'><tr><td width='600'>" . $row["Joketext"] . "</td></tr></table><p>");
    }
    
    endif;
    ?>
    <?php
      // If a joke has been submitted,    
      // add it to the database.    
      if ("SUBMIT" == $submitjoke) {
       $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>");      }    }
    ?>
    
    
    <P><font size="2" face="verdana"><A HREF="<?php echo($PHP_SELF); ?>?addjoke=1">Add Your Jokes!</A><br>
    <A HREF="<?php echo($PHP_SELF); ?>">View All The Jokes.</A></font></P>
    Thanks,
    Justin Sampson

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is an interesting thing I also want to know.

    Since if the visitor reloads, it's like they submit new data (the browser then sends the data again!)

    So basically either you have an unique field (which not this case) or check the input with the latest input (which is possible, but not a very nice technic).

    So if anyone has a better way, please mention. Thanks
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By adding $submitjoke = "";
    It resets the value of $submitjoke to nothing so if the user hits reload the variable $submitjoke will not hold the value "SUBMIT" anymore so it won't add the joke again.


    <?php
    // If a joke has been submitted,
    // add it to the database.
    if ("SUBMIT" == $submitjoke) {
    $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>");
    }
    $submitjoke = "";
    }

    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice, Freddy!
    Have to add this technique into my note
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although it doesn't make a difference, a 'cleaner' way of what freddy did is using unset

    unset($submitjoke);

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah why yes, nice call! Built in functions vs hacked code = always better!
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    Freddy, I can't get your one to work but robp's works fine?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •