form didnt working with mysql - php

this is the html
<form action="addadd.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
First Name<br>
<label for="firstname"></label>
<input type="text" name="firstname" id="firstname" />
</p>
<p>
Last Name<br>
<label for="lastname"></label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
Mobile<br>
<label for="mobile"></label>
<input type="text" name="mobile" id="mobile" />
</p>
<p>
Email<br>
<label for="email"></label>
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
<input type="reset" name="button3" id="button3" value="Reset" />
</p>
</form>
This is the php
database connection
<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("connection to database failed".mysql_error());
}
$dataselect = mysql_select_db("qoot",$con);
if(!$dataselect)
{
die("Database namelist not selected".mysql_error());
}
?>
<?php
$unm = $_SESSION['name'];
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$ema=$_POST['email'];
$mob=$_POST['mobile'];
?>
<?php
$qry=mysql_query("INSERT INTO address( firstname, lastname, mobile, email)VALUES('$fname',$lname','$ema','$mob')", $con);
?>
Now the problem is that this is inserting nothing in to my database.
What else can i try in order to check where things go wrong?
updated database connection details

Place your tablename address in backticks
Using mysqli_
<?php
/start session
session_start();
//establish connection
$con = mysqli_connect("localhost","root","","qoot");
if(!$con)
{
die("connection to database failed".mysqli_error($con));
}
//read the values from form
$unm = $_SESSION['name'];
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$ema=$_POST['email'];
$mob=$_POST['mobile'];
?>
<?php
//insert to database
$qry=mysqli_query($con,"INSERT INTO `address` ( firstname, lastname, mobile, email)VALUES('$fname',$lname','$ema','$mob')") or die(mysqli_error($con));
?>
P.S I'd say you are at risk of mysql injection, check here How can I prevent SQL injection in PHP?. You should really use prepared statements to avoid any risk.

Related

value from formnot entering into database in php

code is not coming into signup_ad.php
signup_ad.php:
<? php
mysql_connect("localhost","root","") or die("not connected");
mysql_select_db("sample") or die("no db found");
if(isset($_POST)
{
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$query= "insert into signup(fname,lname) values('$fname','$lname')";
$result=mysql_query($query);
if (!$result) {
$message='invalid query';
die($message); }
}
?>
value is not entering into db..and also not giving any error. Do i need to include my signup_ad.php file ?
signup.php:
<form action="signup_ad.php" method="post" >
<label>
First Name
</label>
<input type="text" required autocomplete="off" name="fname" />
<label>
Last Name
</label>
<input type="text" required autocomplete="off" name="lname"/>
<button type="submit" class="button button-block" name="btn-signup" value="submit" action="login.php" />Get Started</button>
You forget a quotation before semicolon:
$query= "insert into signup(fname,lname) values('$fname','$lname')";

Information is not saved in mysql database after submitting in form

This is the code for saving the information into mysql database from a FORM.
In the HTML section the form is being handled i.e. retrieving required data from user.
In the PHP section storing data has been handled.
But the problem is it doesn't store data.
I'm using XAMPP server.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="signup.php" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>
I don't understand what can be the problem.
Thanks all. I got the problem.
Actually the sequence of the column in my database was not matching with the query in php code.
I have solved this by changing the variable sequence in the query which is maintained in the database.
$query = mysql_query("INSERT INTO user (`name`, `mail`, `pass`, `address`, `phone`)VALUES('".$name."', '".$mail."', '".$pass."', '".$address."', '".$phone."')");
Here is the code and it will work for your..
I have passed connection link in your mysql_query. and used PHP_SELF for current page.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')",$connection);
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>

direct content of php file is getting displayed on web browser through an html form using wamp server [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 8 years ago.
I am using wamp server and trying to create simple sign up page with html forms and php script; but the problem is whenever I am hitting on submit button on html page, it is directly showing the entire content of php file in next browser instead of executing the php file.
the following is my form code(index.php)
<form id="login" action="register.php" method="post">
<p>
<label for="first name" >First Name</label>
<input type="text" name="fname" value="" />
</p>
<p>
<label for="last name" >Last Name</label>
<input type="text" name="lname" value="" class="radius2" />
</p>
<p>
<label for="gender" >Gender</label>
<input type="text" name="gender" />
</p>
<p>
<label for="username" >Email</label>
<input type="text" name="username" />
</p>
<p>
<label for="password" >Password</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" name="submit">Login</input>
</p>
</form>
the following is my php code
$bd = mysqli_connect("localhost", "root","yash1991","shaunak") or die("Could not connect database");
echo" hello1";
mysqli_query($bd,"INSERT INTO users (fname, lname, gender, email_id, username, password) VALUES ("$fname", "$lname", "$gender", "$username", "$password")");
mysqli_close($bd);
?>
I suppose that you running WAMP server, and stored your files (index.php & register.php) in folder inside "C:\wamp\www\" and you access your index page using url "127.0.0.1/stack/index.php"
Check your index.php file with the following code:
<form id="login" action="register.php" method="post">
<p>
<label for="First Name">First Name</label>
<input type="text" name="fname" value="" />
</p>
<p>
<label for="last Name">Last Name</label>
<input type="text" name="lname" value="" class="radius2" />
</p>
<p>
<label for="gender">Gender</label>
<input type="text" name="gender" />
</p>
<p>
<label for="username">Email</label>
<input type="text" name="username" />
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" name="submit" value="Login" />
</p>
</form>
Check your register.php file with the following code:
<?php
session_start();
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$gender=$_POST['gender'];
$username=$_POST['username'];
$password=$_POST['password'];
echo "hello ";
$bd= mysqli_connect("localhost","root","yash1991","shaunak") or die ("Could not connect DB");
$try = mysqli_query($bd, "INSERT INTO users (`fname`, `lname`, `gender`, `email_id`, `password`) VALUES ('$fname', '$lname', '$gender', '$username', '$password')");
if($try === false)
{
echo '<br />error - ';
echo mysqli_error($bd);
} else {
echo '<br />all good';
}
mysqli_close($bd);
?>

Why isn't my insert data code working?

I am trying to insert data into a database mysql phpAdmin.
My webhost is 000webhost.
My connection to mysql database code:
<?PHP
$mysql_host = "mysql2.000webhost.com";
$mysql_database = "*********";
$mysql_user = "********";
$mysql_password = "**********";
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
if (!$dbcon) {
die('error connecting to database');
}
echo ('You have connected successfully');
?>
My insert data code:
<?PHP
if (isset($_POST['submitted'])) {
include('connect_mysql.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
if (!mysql_query($dbcon, $sqlinsert)) {
die('error inserting new record');
} // end of nested if statement
$newrecord = "1 record added to the database";
}
?>
<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<h1>Insert Data into DB</h1>
<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>
Instead of letting me put it into the database it brings me to this page
http://error404.000webhost.com/?
try change
<label>First Name: <input type="text name="fname" /></label>
<label>Last Name: <input type="text name="lname" /></label>
to
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
and also insert query and connection to
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
First change below code:
$dbcon = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
To :
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
Then change below code:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('fname', 'lname')";
To:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
can u please change this line?
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text name="fname" /></label>
<label>Last Name: <input type="text name="lname" /></label>
</fieldset>
to
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
you are setting wrong attributes so its not getting the values..
after that, u have to update your sql query:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
It DID have to be mysqli
my problem was the - had to be changed to an _ on <form method="post" action="insert-data.php">
working code:
<?PHP
if (isset($_POST['submitted'])) {
include('connect_mysql.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
if (!mysqli_query($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "1 new record added to the database";
}
?>
<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<h1>Insert Data into DB</h1>
<form method="post" action="insert_data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>

Adding a record from html form to oracle database

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'];

Categories