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.
Related
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="duration" placeholder="Enter duration">
<input type="text" name="budget" placeholder="Enter Budget">
<input type="text" name="keyskills" placeholder="Enter Skills">
<input type="text" name="jobdescription" placeholder="Enter Job Description">
<input type="text" name="edate" placeholder="Click to enter expiry date">
<input type="text" name="cdexmin" placeholder="Enter Minimum Experience">
<input type="text" name="cdexmax" placeholder="Enter Maximum Experience">
<input type="submit">
</form>
<?php
if(isset($_POST['submit'])) {
try {
// Establish server connection and select database
$username = $_SESSION['username'];
$stmt = $db->prepare("SELECT * FROM employer INNER JOIN company ON employer.cid = company.cid WHERE employer.username='$username' ");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$cid=$row['cid'];
$eid = $row['eid'];
$duration = $_POST['duration'];
$budget = $_POST['budget'];
$keyskills = $_POST['keyskills'];
$jobdescription = $_POST['jobdescription'];
$edate = $_POST['edate'];
$cdexmin = $_POST['cdexmin'];
$cdexmax = $_POST['cdexmax'];
$stmt = $db->prepare("INSERT INTO job(cid,eid,duration,budget,keyskills,jdesc,edate,cdexmin,cdexmax) values('$cid','$eid','$duration','$budget','$keyskills','$jobdescription','$edate','$cdexmin','$cdexmax') ");
$stmt->execute();
echo "JOB POSTED SUCCESSFULLY";
} catch(PDOException $e) {
echo "Error occurs:". $e->getMessage();
}
}
?>
Here is my code that I just created for a sample form that is trying to insert the values into database. My problem is that the values are not entering the database.
Why is it not working? Is there an syntax error I can't find?
Page parsing is easily done and it's not showing any errors but values are not entering the database.
You are using $_POST['submit'] but there is no any input with this name.
Add name to your input type submit as follow
<input type="submit" name="submit">
Change
<input type="submit">
to
<input type="submit" name="submit">
You have change in code. Add NAME attribute for POST form.
<input type="submit" name="submit">
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>
I have a HTML form that I want to add a record to an Oracle database when somebody hits submit. The table is hooking up somewhat, the problem is that when somebody submits their information they come up as NULL values in the database table.
HTML:
<form name="myForm" action="/Add_File.php" onsubmit="return validateForm()" method="post"><!--Form-->
<fieldset>
<label class ="label1" for "name">First Name: </label>
<input type="text" name="fname"><br />
<label class ="label1" for "name">Surname: </label><input type="text" name="sname"><br/>
<label for="email">E-mail Address: </label><input type="text" name="email"><br />
<label for "address">Address: </label> <input type="text" name="address"><br />
<label class="label" for "Password">Select a Password: </label> <input type="password" name="pass"><br />
<label class="label" for "Password">Retype-Password:</label> <input type="password" name="pass2"><br />
</fieldset>
<fieldset><input class="button" type="submit" onclick="message()" value="Submit"/>
<input class="button" type="reset" value="reset form" onclick="myFunction()"/>
</fieldset>
</form>
PHP code:
$dbuser = "scott";
$dbpassword = "tiger";
$db = "orabis";
$conn = oci_connect($dbuser,$dbpassword,$db);
if (!$conn){
echo "Connection error";
exit;
}
$fname=$_POST['First_Name'];
$sname=$_POST['Surname'];
$email=$_POST['Email_Address'];
$address=$_POST['Address'];
$selpass=$_POST['Select_A_Password'];
$confirm=$_POST['Retype_Password'];
$sql = "INSERT INTO Become_A_Member_110385461(First_Name,Surname,Email_Address,Address,Select_A_Password,Retype_Password)
VALUES ('".$fname."','".$sname."', '".$email."', '".$address."','".$selpass."', '".$confirm."')";
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
echo "Error in preparing the statement";
exit;
}
oci_execute($stmt, OCI_DEFAULT);
print "Record Inserted";
oci_commit($conn);
oci_close($conn);
change like this
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$email=$_POST['email'];
$address=$_POST['address'];
$selpass=$_POST['pass'];
$confirm=$_POST['pass2'];
I want to make a text field and button that will allow the user to fetch his details on the text fields instead of writting his details every time he wants to make a new reservation.
like in this picture:
http://oi41.tinypic.com/23ie70j.jpg
I tried to make this but with my code but gives me double forms one with the details and one without.
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$q = "SELECT * FROM tabe WHERE the_email = '$email'";
$run = mysql_query($q );
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}}
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
//limit the query to one entry :)
$q = "SELECT * FROM tabe WHERE the_email = '$email' LIMIT 1";
$run = mysql_query($q );
//check if email is registered
if(mysql_num_rows($run)>0)
{
//display filled up form
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}
}
//display blank form
else{
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<?php
}
}
?>
You're getting 2 forms because you're echoing one form if $_POST['submit'] is set and then another one regardless of anything. Print the second form only if $_POST['submit'] is not set. Since your code is so poorly written I will just give you an example:
if(isset($_POST['submit'])){
PRINT FETCHED FORM
}else{
PRINT EMPTY FORM
}
This, however, is not the "right" way to go. What people actually do is have variables null'd at start and then fill them up with data if there's a request and have a single form written in the file with input values as those variables.
I have an HTML form for submitting and retrieves data from MySQL database with two buttons, "Save/Submit" and "New/Reset"
It fetch data correctly from MySQL database but when I click on New/Reset button for new contact entry it couldn't clear forms text fields. My HTML and PHP codes are as under:
<?php
//Database Connection file.
include'connect.php';
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'");
While($result = mysql_fetch_assoc($sql)){
$fname = $result['fname'];
$lname = $result['lname'];
$email = $result['email'];
$contact = $result['contact'];
}
if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&&
isset($_POST['contact'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname',
'$email', '$contact')")){
echo'Contact Save Successfully.';
}else{
echo'Contact not save.';
}
}
?>
<html>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>">
//Clean all fields of forms for new entry.
<input type="reset" value="New">
//Save or submit form data into mysql database
<input type="submit" value="Save">
</form>
</html>
You can do this easily by using jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#btnReset").click(function(){
$("#fname").val("");
$("#lname").val("");
$("#email").val("");
$("#contact").val("");
});
});
</script>
</head>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>" id="fname">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>" id="lname">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>" id="email">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>" id="contact">
//Clean all fields of forms for new entry.
<input type="reset" value="New" id="btnReset">
//Save or submit form data into mysql database
<input type="submit" value="Save" id="btnSave">
</form>
</html>