开发者

Refreshing form -> uploading data

I have a problem with my PHP form. Whenever I refresh the page, the old data automatically inserted to my database. My codes are:

<?php
   if(isset($_GET['send'])){
      isset($_GET['name'])?$name = $_GET['name']:"";
      isset($_GET['score'])?$score = $_GET['score']:0;

      $con = mysql_connect('localhost','root','') or die(mysql_error());
             mysql_select-db('student',$con) or die(mysql_error());
      $qry = mysql_query('INSERT INTO student(name, score) VALUES('$name', $score)') or die(mysql_error());

      $display = mysql_query('SELECT * FROM student',$con) or die(mysql_error());
      echo '<table border=1>';
      while($rows = mysql_fetch_array($display)){
          echo '<tr>';
          echo "<td>$rows['id']</td>";
          echo "&l开发者_JAVA百科t;td>$rows['name']</td>";
          echo "<td>$rows['score']</td>";
          echo '</tr>';
      } 
      echo '</table>';
    }
    ?>

please help me solve this problem.


A common way to prevent duplicate form submission is to make use of the Post/Redirect/Get Pattern.

You would need to change your forms method to Post then. After successful form submission you redirect to the form again but making the redirect a get request. The form will be reset then (empty values).

Edit:

Now as I see it, your script can actually do something similar: After insertion into the mysql Database you can redirect it to itself removing the get parameters:

<?php
if(isset($_GET['send'])){
  isset($_GET['name'])?$name = $_GET['name']:"";
  isset($_GET['score'])?$score = $_GET['score']:0;

  $con = mysql_connect('localhost','root','') or die(mysql_error());
         mysql_select-db('student',$con) or die(mysql_error());
  $qry = mysql_query('INSERT INTO student(name, score) VALUES('$name', $score)') or die(mysql_error());
  header('Location: myscriptsurl.php');
  exit;
}

$display = mysql_query('SELECT * FROM student',$con) or die(mysql_error());
echo '<table border=1>';
while($rows = mysql_fetch_array($display)){
  echo '<tr>';
  echo "<td>$rows['id']</td>";
  echo "<td>$rows['name']</td>";
  echo "<td>$rows['score']</td>";
  echo '</tr>';
} 
echo '</table>';
?>


So you have a problem. And since you cannot avoid a refresh of the screen...

If you are doing a form post, you might consider sending a location header AFTER you inserted the record:

<form action="process.php" method="POST">
<input type="text" name="number">
<input type="sumbit">
</form>

then from process.php: // do you usual inserts in the database based on the post

header("Location: http://www.example.com/thanks.php");
// do not forget the exit, since your script will run on without it.
exit;

In that way your script will process the posting, and then redirects the browser to thanks.php. A reload of thanks.php will not result in a fresh db insert.


U have used GET method so every time page refresh it will fetch the value from URL. Try using POST method...It will solve your Problem and don't forget to Put Condition for POST

if(isset($_POST))
{
   /* Your Insert Code */

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜