刷新形式->上传数据

发布于 2024-11-18 19:46:32 字数 934 浏览 2 评论 0原文

我的 PHP 表单有问题。每当我刷新页面时,旧数据就会自动插入到我的数据库中。我的代码是:

<?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 "<td>$rows['name']</td>";
          echo "<td>$rows['score']</td>";
          echo '</tr>';
      } 
      echo '</table>';
    }
    ?>

请帮我解决这个问题。

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 "<td>$rows['name']</td>";
          echo "<td>$rows['score']</td>";
          echo '</tr>';
      } 
      echo '</table>';
    }
    ?>

please help me solve this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小墨 2024-11-25 19:46:32

防止重复表单提交的常见方法是使用 Post/Redirect/Get 模式< /a>.

然后,您需要将表单方法更改为发布。成功提交表单后,您再次重定向到表单,但使重定向成为 get 请求。然后表格将被重置(空值)。

编辑:

现在,据我所知,您的脚本实际上可以执行类似的操作:插入 mysql 数据库后,您可以将其重定向到自身,删除 get 参数:

<?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>';
?>

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>';
?>
草莓味的萝莉 2024-11-25 19:46:32

所以你有问题。
由于您无法避免刷新屏幕...

如果您正在执行表单发布,您可能会考虑发送位置
插入记录后的标头:

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

然后来自 process.php:
// 你通常会根据帖子插入数据库吗?

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

这样你的脚本就会处理帖子,然后重定向
浏览器访问 thanks.php.
重新加载 thanks.php 不会导致新的数据库插入。

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.

情场扛把子 2024-11-25 19:46:32

您使用了 GET 方法,因此每次页面刷新时都会从 URL 中获取值。
尝试使用 POST 方法...它将解决您的问题,并且不要忘记为 POST 设置条件

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

}

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 */

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文