刷新形式->上传数据
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
防止重复表单提交的常见方法是使用 Post/Redirect/Get 模式< /a>.
然后,您需要将表单方法更改为发布。成功提交表单后,您再次重定向到表单,但使重定向成为 get 请求。然后表格将被重置(空值)。
编辑:
现在,据我所知,您的脚本实际上可以执行类似的操作:插入 mysql 数据库后,您可以将其重定向到自身,删除 get 参数:
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:
所以你有问题。
由于您无法避免刷新屏幕...
如果您正在执行表单发布,您可能会考虑发送位置
插入记录后的标头:
然后来自 process.php:
// 你通常会根据帖子插入数据库吗?
这样你的脚本就会处理帖子,然后重定向
浏览器访问
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:
then from process.php:
// do you usual inserts in the database based on the post
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.您使用了 GET 方法,因此每次页面刷新时都会从 URL 中获取值。
尝试使用 POST 方法...它将解决您的问题,并且不要忘记为 POST 设置条件
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