提交到mySQL时,PHP保存了两次

时间:2016-03-07 06:50:41

标签: php mysql wordpress

我使用wordpress作为我网站的平台,并编写以下代码将表单提交到mySQL数据库。问题是,每当我提交一个from时,它在数据库上出现两次。我想知道是什么导致了这个问题。

<?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>

1 个答案:

答案 0 :(得分: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>