PHP saved twice when submitting to mySQL -


i using wordpress platform website , wrote following code submit form mysql database. problem is, whenever submit from, appears twice on database. wondering caused 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 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 redirect header, avoid duplication. time may hit twice, avoid use following header.

header('location: page.php'); 

you can specify 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 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> 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -