PHP saved twice when submitting to mySQL - php

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

Related

Update mysql table with one query the values from an automated generated form

I have the following form automatically generated in a WHILE using $i++.
<form method="POST" id="form" >
<input type="text" name="id1" value="1" />
<input type="text" name="q1" value="5" />
<input type="text" name="a1" value="p" />
<input type="text" name="id2" value="4" />
<input type="text" name="q2" value="3" />
<input type="text" name="a2" value="t" />
<input type="text" name="id3" value="8" />
<input type="text" name="q3" value="7" />
<input type="text" name="a3" value="z" />
.....................
</form>
I insert the values into mysql using:
$id1=$_POST['id1'];
$q1=$_POST['q1'];
$a1=$_POST['a1'];
$query = "UPDATE table SET answer = '$a1' WHERE question = '$q1' and id= '$id1'";
mysqli_query($con,$query) or die(mysqli_error());
$id2=$_POST['id2'];
$q2=$_POST['q2'];
$a2=$_POST['a2'];
$query = "UPDATE table SET answer = '$a2' WHERE question = '$q2' and id= '$id2'";
mysqli_query($con,$query) or die(mysqli_error());
$id3=$_POST['id3'];
$q3=$_POST['q3'];
$a3=$_POST['a3'];
$query = "UPDATE table SET answer = '$a3' WHERE question = '$q3' and id= '$id3'";
mysqli_query($con,$query) or die(mysqli_error());
..........................
It's working just fine but how could I insert the values with only one query and WHILE or foreach?
I tried to do like this but something is wrong:
$stmt = $this->mysqli->prepare("UPDATE table SET answer=? WHERE question=? and id =?");
foreach ($_POST['id'] as $i => $n){
$stmt->bind_param("sss", $n, $_POST['question'][$i], $_POST['answer'][$i], $id);
$stmt->execute();
}
You can give same name to mutliple inputs and use [] so that it be access using 0,1..etc. So your form will look like below :
<form method="POST" id="form" >
<input type="text" name="id[]" value="1" />
<input type="text" name="question[]" value="5" />
<input type="text" name="answer[]" value="p" />
<input type="text" name="id[]" value="4" />
<input type="text" name="question[]" value="3" />
<input type="text" name="answer[]" value="t" />
.....................
</form>
And then access same in your php page like below :
$id= isset($_POST['id']) ? $_POST['id'] : "" ;//get post datas
$question= isset($_POST['question']) ? $_POST['question'] : "" ;
$answer= isset($_POST['answer']) ? $_POST['answer'] : "" ;
$stmt = $this->mysqli->prepare("UPDATE table SET answer=? WHERE question=? and id =?");
//loop through ids
foreach ($id as $key=>$value){
//answer[0],answer[1]..
$stmt->bind_param("sss", $answer[$key], $question[$key], $value);
$stmt->execute();
}

Displaying a column that a user created in a form

Im trying to display a single column from where a user created a form, i have a table and user sessions set up. I need it so that only that column is displayed for the user that created it.
This is the form
<form action="core/process.php" method="post" id="registration" >
<input type="hidden" name="formID" value="Product_Tracker" />
<input type="hidden" name="id_user" value="<?php echo $_SESSION['name_of_user']; ?>" />
<p>Name of product:<input type="text" name="Name of Product" class="input" />
<p>Please select the tests that were done on the product.</p>
<p>In Circuit Test (ICT): Yes: <input type="radio" name="ICT" value="yes" /> No: <input type="radio" name="ICT" value="no" /></p>
<p>Visual Inspection: Yes: <input type="radio" name="Visual Inspection" value="yes" /> No: <input type="radio" name="Visual Inspection" value="no" /></p>
<p>XRAY: Yes: <input type="radio" name="XRAY" value="yes" /> No: <input type="radio" name="XRAY" value="no" /></p>
<p>Automated Optical Inspection (AOI): Yes: <input type="radio" name="AOI" value="yes" /> No: <input type="radio" name="AOI" value="no" /></p>
<!--<p>Checkbox1 <input type="checkbox" name="checkbox" value="checkbox1" /> Checkbox2: <input type="checkbox" name="checkbox" value="checkbox2" /></p>-->
<input type="submit" value="Submit" />
<p>
<a href='access-controlled.php'>Back</a>
</p>
</form>
</div>
</body>
</html>
<?php VDEnd(); ?>
Ive tried this but it doesnt work,
$con = mysql_connect("","","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("database",$con);
$result = mysql_query("SELECT id_user, Name_of_product FROM Product_Tracker WHERE id_user=$_SESSION['name_of_user']");
while ($row = mysql_fetch_assoc($result)) {
echo $row['Name_of_Product'];
echo "<br />";
this did it;
mysql_select_db("database",$con);
$id=$_SESSION['name_of_user'];
$result = mysql_query("SELECT id_user, Name_of_product FROM Product_Tracker WHERE id_user='$id'");
while ($row = mysql_fetch_array($result))
{
echo $row['Name_of_product'] . "<br/>";
}
mysql_query($query);
mysql_close($con);
?>
have you tried this
$id=$_SESSION['name_of_user'];
$result = mysql_query("SELECT id_user, Name_of_product FROM Product_Tracker WHERE id_user='$id'");

how to insert value of two arrays together in database

Here i my form i am having four checkboxes with each having a textbox for its description
<form action="" method="post">
<input type="checkbox" name="days[]" value="Sunday"/>Sunday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Monday"/>Monday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Tuesday"/>Tuesday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Wednesday"/>Wednesday
<input type="text" name="description[]" />
<input type="submit" name="submit" value="submit">
</form>
Here the user can choose any textbox and can write its description...now at the time of insertion...i want to insert only those values where checkboxes are checked...
For e.g: If i choose monday and tuesday then monday and tuesday should be inserted along with their respectice textboxes....my problem is that when i am submitting then the checkbox values are going right but only single description is getting inserted....here is mmy php script...
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$i];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
?>
Can anyone help??
First of all, why aren't you making your life easier; and giving them a normal non array name?
What you can do is;
if ((isset($_POST["days"]) && isset($_POST["description"])) {
$days = $_POST["days"];
$desc = $_POST["description"];
$len = count($days);
for ($i = 0; $i <= $len-1; $i++) {
$noID = filter_var($days[$i], FILTER_SANITIZE_NUMBER_INT);
echo $days[$noID] . " " . $desc[$noID];
}
}
Check if that will work for you :)
Tested & Working:
HTML
<input type="checkbox" name="days[0][day]" value="Sunday"/>Sunday <input type="text" name="days[0][value]" />
<br/><input type="checkbox" name="days[1][day]" value="Monday"/>Monday <input type="text" name="days[1][value]" />
<br/><input type="checkbox" name="days[2][day]" value="Tuesday"/>Tuesday <input type="text" name="days[2][value]" />
<br/><input type="checkbox" name="days[3][day]" value="Wednesday"/>Wednesday <input type="text" name="days[3][value]" />
PHP
<?PHP
$arr = $_POST["days"];
for ($i = 0; $i < count($arr); $i++) {
if (!empty($arr[$i]['day'])) {
echo $arr[$i]['day'] . " and.... its value is " . $arr[$i]['value'];
}
}
?>
In php
<?php
if (isset($_POST['chk_group'])) {
$dayArray = $_POST['days'];
$decArray =$_POST['description'];
for ($i=0; $i<count($dayArray); $i++) {
$dayz=$dayArray[$i];
$description=$decArray[$i];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
?>
try to use description[dayname] in the form and use $description=$_POST["description"][$dayz] in processing, you have array indexing issue because checkbox are just not send if not checked.
here i my form i am having four checkboxes with each having a textbox for its description
<form action="" method="post">
<input type="checkbox" name="days[]" value="Sunday"/>Sunday <input type="text" name="description[Sunday]" />
<br/><input type="checkbox" name="days[]" value="Monday"/>Monday <input type="text" name="description[Monday]" />
<br/><input type="checkbox" name="days[]" value="Tuesday"/>Tuesday <input type="text" name="description[Tuesday ]" />
<br/><input type="checkbox" name="days[]" value="Wednesday"/>Wednesday <input type="text" name="description[Wednesday]" />
<input type="submit" name="submit" value="submit">
</form>
and php:
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$dayz];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
this may help
$str = '';
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$i];
$str .= "(".$dayz.", ".$description.")";
if ((count($_POST["days"])-1) < $i) {
$str .= ',';
}
}
mysql_query("insert into transport_two (transport_id,name) values ".$str) ;
Firstly name each text input and check-box with different name.
<form action="" method="post">
<input type="checkbox" name="days1" value="Sunday"/>Sunday
<input type="text" name="days1_1" /><br/>
<input type="checkbox" name="days2" value="Monday"/>Monday
<input type="text" name="days2_2" /><br/>
<input type="checkbox" name="days3" value="Tuesday"/>Tuesday
<input type="text" name="days3_3" /><br/>
<input type="checkbox" name="days4" value="Wednesday"/>Wednesday
<input type="text" name="days4_4" />
<input type="submit" name="submit" value="submit">
</form>
Use this code for checking which check-box are checked.
<?php
if(isset($_POST["submit"]))
{
for($i=0;$i<$number_of_days;$i++)
{
$dayz="days".$i;
$description=$dayz."_".$i;
if(isset($dayz))
{
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
}
?>

How to get the submitted form data in mysql database table

I wrote this following piece of code & it must echo "Sign up successful" if pswd = retype pswd
& later INSERT the following values into the TABLE (WHICH ISN'T). It simply echo's Sign up successful but the values are not inserted into TABLE. I'm pretty much sure that i failed in connecting to DB in my PHP code, please correct my code!
My Code is as follows:
<?php
session_start();
include('header.php');
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '1234';
$mysql_db = "my_db";
if (!mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !mysql_select_db($mysql_db))
{
die(mysql_error());
}
if( isset( $_REQUEST['namevar'] ) ){
$nmvar = $_REQUEST['namevar'];
$email = $_REQUEST['name1'];
$psvar = $_REQUEST['p1'];
$cpsvar = $_REQUEST['p2'];
$gender = $_REQUEST['r1'];
$clvar = $_REQUEST['t2'];
$plvar = $_REQUEST['t1'];
if($psvar == $cpsvar)
{
$sql = "INSERT INTO `users` (name,email,password,confirm password,gender,college,place)
VALUES ('$nmvar','$email','$psvar','$cpsvar','$gender','$clvar','$plvar')";
mysql_query($sql);
echo "Signup successful";
}
else
{
echo "Password Mismatch";
}
}
?>
<form action="" method="post">
Name:
<input type="text" name="namevar" /><br/>
E-mail:
<input type="text" name="name1" /><br/>
Password:
<input type="password" name="p1" /><br/>
Confirm Password:
<input type="password" name="p2" /><br/>
Gender:
<input type="radio" name="r1" />
Male
<input type="radio" name="r1" />
Female
<br/>
Location:
<input type="text" name="t1" /><br/>
College:
<input type="text" name="t2" /><br/>
<input type="submit" value="submit" /><br/>
<input type="reset" value="reset" />
</form>
You should use the mysql_real_escape_string() function on the data before submitting it into the database. Otherwise, your code is at risk for SQL injection.

insert proccess whith looping in php

firstly sorry my english is bad
then
i have problem
i try to make lopping insert into table but i have problem i can't make array for inputs
to can insert in table with looping from another table
maybe u can't understand me but look to the code and u will understand my problem
$select = mysql_query("SELECT * FROM table_name ");
?>
<form method="post" action="">
<?
while($row =mysql_fetch_array($select))
{
if($_POST['add'])
{
$updpol = mysql_query("insert into table_name2 (yes,no,maybe,g_id)
values
('".$_POST['yes']."','".$_POST['no']."','".$_POST['maybe']."','".$row['id']."')
")
}
else{
?>
<input type="checkbox" name="yes" value="1" />
<br />
<input type="checkbox" name="no" value="1" />
<br />
<input type="checkbox" name="maybe" value="1" />
<?
}
?>
<input type="submit" name="add_poll" value="submit" />
</form>
in your php code , your checking if:
if($_POST['add'])
While there's no input named add in your form.
I believe you meant:
if($_POST['add_poll'])
In order to have multiply instances of inputs , you can do something like:
<input type="checkbox" name="yes[]" value="1" />
<br />
<input type="checkbox" name="no[]" value="1" />
<br />
<input type="checkbox" name="maybe[]" value="1" />
So , now , when posted:
$_POST['yes'] is an array of all the checkboxes.
And as an array , it starts with $_POST['yes'][0] and continues $_POST['yes'][1] and so on.
Therefore:
$select = mysql_query("SELECT * FROM table_name ");
?>
<form method="post" action="">
<?
$i = 0;
while($row =mysql_fetch_array($select))
{
if($_POST['add_poll'])
{
$updpol = mysql_query("insert into table_name2 (yes,no,maybe,g_id)
values
('".$_POST['yes'][$i]."','".$_POST['no'][$i]."','".$_POST['maybe'][$i]."','".$row['id']."')
");
$i++;
}
else{
?>
<input type="checkbox" name="yes[]" value="1" />
<br />
<input type="checkbox" name="no[]" value="1" />
<br />
<input type="checkbox" name="maybe[]" value="1" />
<?
}
?>
<input type="submit" name="add_poll" value="submit" />
</form>

Categories