所以我有一个表单,一旦提交到另一个页面,在该页有一个查询插入数据到数据库,虽然当我刷新该页它仍然输入空数据,我怎么能使它这样,如果页面刷新不输入数据或当页面被访问而不输入表单。
你需要分离出你的关注,通常浏览器会总是保存提交的数据到$_POST
数组,所以刷新一个页面,有数据提交给它将保留所有提交的数据,很容易导致多次提交。
要解决这个问题,您需要按照以下方式布局逻辑:
<page that has the HTML form on it>
||
'/
<page that saves data but does NOT output anything to the browser.>
This page will then user a PHP header to redirect to the next page...
||
'/
<page which outputs to the browser but writes nothing to the database>
So this page can be refreshed as much as you like, you will not get
multiple writes to the database unless you repeat submitting the form
via page 1
我们不能解决为什么你有空数据输入到你的数据库没有看到你的代码,但它可能会更明智的你阅读其他堆栈溢出的答案,并非常仔细地检查你的代码If
语句不一致。