[英]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.我使用 wordpress 作为我网站的平台,并编写了以下代码以向 mySQL 数据库提交表单。 The problem is, whenever I submit a from, it appears twice on the database.
问题是,每当我提交一个 from 时,它在数据库中出现两次。 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&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您可以指定实际文件名(或)URL 而不是 page.php
Your PHP Source Code should be你的 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&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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.