2016-03-07 27 views
1

我使用wordpress作爲我的網站的平臺,並且寫了下面的代碼將表單提交給mySQL數據庫。問題是,無論何時我提交一個from,它都會在數據庫中出現兩次。我想知道是什麼導致了這個問題。PHP在提交給mySQL時保存了兩次

<?php 
echo $sql; 
$name=''; 
$Gender=0; 
$HPV=0; 
$HIV=0; 
$Herpes=0; 
$symptoms=0; 
$diagnosed=0; 
$A=0; 
$E=0; 
$C=0; 
$QNumber=0; 

?> 
<?php if (isset($_POST['submit'])) { 
$db_host = 'localhost'; 
$db_user = ''; 
$db_pwd = ''; 
$database=''; 

mysql_connect($db_host, $db_user, $db_pwd); 

mysql_select_db($database); 
$name= $_POST['username']; 
if (isset($_POST['Female'])) {$Gender=1;} 
if (isset($_POST['Male'])) {$Gender=2;} 
if (isset($_POST['Herpes'])) {$Herpes=1;} 
if (isset($_POST['HPV'])) {$HPV=1;} 
if (isset($_POST['HIV'])) {$HIV=1;} 
if (isset($_POST['Symptoms'])) {$symptoms=1;} 
if (isset($_POST['Diagnosed'])){ $diagnosed=1;} 
if (isset($_POST['Awareness'])){ $A=1;} 
if (isset($_POST['Education'])){ $E=1;} 
if (isset($_POST['Counseling'])){ $C=1;} 
$Qnumber=$_POST['Number']; 

$sql = "INSERT INTO QuestionAnswer (name, HPV,HIV,Herpes,Reach,Gender,Awareness,Education,Counseling,Symptoms,Diagnosed) 
VALUES ('" . $name . "',$HPV,$HIV,$Herpes,$QNumber,$Gender,$A,$E,$C,$symptoms,$diagnosed)"; 
mysql_query($sql); 


mysql_close(); 

} ?> 
<h2>Data Entery</h2> 
<form enctype="multipart/form-data" method="post" action="" > 
Name: <input name="username" type="text" /> 

Gender : <input name="Female" type="checkbox" />Female <input name="Male" type="checkbox" />Male 

Diseases: <input name="Herpes" type="checkbox" />Herpes <input name="HPV" type="checkbox" />HPV <input name="HIV" type="checkbox" /> HIV 

Symptoms: <input name="Symptoms" type="checkbox" /> Yes 

Diagnosed: <input name="Diagnosed" type="checkbox" /> Yes 

Number of Q&amp;A : <input name="Number" type="text" /> 

Awareness: <input name="Awareness" type="checkbox" /> Yes 

Education: <input name="Education" type="checkbox" /> Yes 

Counseling: <input name="Counseling" type="checkbox" /> Yes 

<input name="submit" type="submit" value="submit" /> 

</form> 
+0

兩記插入? – devpro

+1

如果你在wordpress上使用['wpdb'](https://codex.wordpress.org/Class_Reference/wpdb),其次請[跳過你的數據庫輸入](https://codex.wordpress.org/Data_Validation ),你的查詢真的是你所放的,你可以有SQL注入與不需要的結果... –

+0

麻煩記錄相同?或者其中一個字段具有缺省值,如果answer是第二個,那麼'if(isset($ _ POST ['submit']))'可能不能正常工作,並且頁面加載導致插入一個記錄,並提交插入反對? – Yazan

回答

1

只需使用重定向標題,避免重複。有一段時間它可能會打兩次,以避免使用以下標題。

header('Location: page.php'); 

可以指定實際文件名(或)URL,而不是page.php文件

你的PHP源代碼應該是

<?php 

$name=''; 
$Gender=0; 
$HPV=0; 
$HIV=0; 
$Herpes=0; 
$symptoms=0; 
$diagnosed=0; 
$A=0; 
$E=0; 
$C=0; 
$QNumber=0; 

if (isset($_POST['submit'])) { 
$db_host = 'localhost'; 
$db_user = ''; 
$db_pwd = ''; 
$database=''; 

mysql_connect($db_host, $db_user, $db_pwd); 

mysql_select_db($database); 
$name= $_POST['username']; 
if (isset($_POST['Female'])) {$Gender=1;} 
if (isset($_POST['Male'])) {$Gender=2;} 
if (isset($_POST['Herpes'])) {$Herpes=1;} 
if (isset($_POST['HPV'])) {$HPV=1;} 
if (isset($_POST['HIV'])) {$HIV=1;} 
if (isset($_POST['Symptoms'])) {$symptoms=1;} 
if (isset($_POST['Diagnosed'])){ $diagnosed=1;} 
if (isset($_POST['Awareness'])){ $A=1;} 
if (isset($_POST['Education'])){ $E=1;} 
if (isset($_POST['Counseling'])){ $C=1;} 
$Qnumber=$_POST['Number']; 

$sql = "INSERT INTO QuestionAnswer (name, HPV,HIV,Herpes,Reach,Gender,Awareness,Education,Counseling,Symptoms,Diagnosed) 
VALUES ('" . $name . "',$HPV,$HIV,$Herpes,$QNumber,$Gender,$A,$E,$C,$symptoms,$diagnosed)"; 
mysql_query($sql); 


mysql_close(); 

header('Location: page.php'); 

} ?> 
<h2>Data Entery</h2> 
<form enctype="multipart/form-data" method="post" action="" > 
Name: <input name="username" type="text" /> 

Gender : <input name="Female" type="checkbox" />Female <input name="Male" type="checkbox" />Male 

Diseases: <input name="Herpes" type="checkbox" />Herpes <input name="HPV" type="checkbox" />HPV <input name="HIV" type="checkbox" /> HIV 

Symptoms: <input name="Symptoms" type="checkbox" /> Yes 

Diagnosed: <input name="Diagnosed" type="checkbox" /> Yes 

Number of Q&amp;A : <input name="Number" type="text" /> 

Awareness: <input name="Awareness" type="checkbox" /> Yes 

Education: <input name="Education" type="checkbox" /> Yes 

Counseling: <input name="Counseling" type="checkbox" /> Yes 

<input name="submit" type="submit" value="submit" /> 

</form>