简体   繁体   中英

PHP saved twice when submitting to mySQL

I am using wordpress as a platform for my website and wrote following code to submit a form to mySQL database. The problem is, whenever I submit a from, it appears twice on the database. I am wondering what caused the problem.

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

Simply use the Redirect Header, to avoid Duplication. Some time it may hit twice, to avoid use the following header.

header('Location: page.php');

You can specify the actual file name (or) URL instead of page.php

Your PHP Source Code should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM