refresh = on在php中提交,如何解决?

gad*_*dss 2 php mysql post

我在php中创建插入过程但我的代码中有问题.当refresh我的页面,它也将submitinsert数据.

这是我的代码:

<form action="/drupal/node/1" method="post">
Name: <input type="text" name="name" />
Price: <input type="text" name="price" />
Minutes: <input type="text" name="minutes" />
<input type="submit" />
</form>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code
mysql_select_db("zain", $con);
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['minutes']))
{
 $val_name = $_POST['name'];
 $val_price = $_POST['price'];
 $val_min = $_POST['minutes'];
 $max_id_sql = mysql_query("SELECT MAX(id) FROM card_category");
 $data = mysql_fetch_array($max_id_sql);
 if ($data[0]==0)
 {
    $val_id = 1;
 }
 else
 {
    $val_id = $data[0] + 1;
 }

 mysql_query("INSERT INTO card_category (id, name, price, minutes) VALUES ($val_id,'$val_name',$val_price,$val_min )");
 $_POST['name'] == NULL;
 $_POST['price'] == NULL;
 $_POST['minutes'] == NULL;
}
$result = mysql_query("SELECT * FROM card_category");

echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>price</th>
<th>mins</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['minutes'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
if (isset($_POST['lastname']))
{
print $_POST['lastname'];
}
mysql_close($con);
?>
Run Code Online (Sandbox Code Playgroud)

我的问题是,我怎么能处理我refresh的页面,它不会submit是数据?

提前致谢

Que*_*tin 10

当你收到POST提交时:

  1. 处理数据
  2. 返回Redirect响应
  3. 从您重定向到的URL上获取浏览器的GET请求
  4. 用HTML回复

如果刷新浏览器,它将重新提交您的PHP不会用来修改数据库的GET请求.

这是POST-REDIRECT-GET模式.此博客条目中有更多详细信息(评论中也包含示例PHP代码).