2016-09-29 31 views
-1

所以我有一個表單提交到另一個頁面,並在該頁面有一個查詢插入數據到數據庫,雖然當我刷新頁面它仍然輸入空數據,如何我可以做到這一點,如果頁面刷新,不輸入數據或頁面訪問時沒有輸入表單。PHP刷新頁面防止數據插入

+0

添加你的代碼,如果你沒有顯示你做了什麼,我們不能猜測出什麼問題。 –

回答

-1

添加if語句來檢查它是否頁面被訪問POST或GET

if (!empty($_POST)){ 
    // Save data to database 
} 

//顯示頁面數據

+0

他說,如果頁面刷新數據寫入數據庫。你認爲這些數據來自哪裏? – andrew

0

你需要單獨出您的問題,一般的瀏覽器將總是保存發佈數據添加到$_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聲明不一致。