isset function not working correctly - php

I am trying to insert records into a table but I am facing a problem on the isset function. I am unable to process the code.
In the following code the else statement is executed instead of the isset function.
<?php
if (isset($_POST['submit'])) {
include_once 'dbh.inc.php';
$firstname = mysqli_real_escape_string($conn, $_POST['firstname']);
$surname = mysqli_real_escape_string($conn, $_POST['surname']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']);
$bdate = mysqli_real_escape_string($conn, $_POST['bdate']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
if(empty($firstname) || empty($surname) || empty($email) || empty($username) || empty($password) || empty($cpassword) || empty($bdate) || empty($gender)) {
header("Location: ../index.php?index=empty");
exit();
} else {
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: ../index.php?index=username already taken");
exit();
} else {
// Hashing the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
//Insert users into the database
$sql = "INSERT INTO users (id, firstname, surname, email, username, password, bdate, gender) VALUES ('', '$firstname', '$surname', '$email', '$username', '$password', '$bdate', '$gender');";
mysqli_query($conn, $sql);
header("Location: ../profile.php");
exit();
}
}
} else {
header("Location: ../index.php?index=error");
exit();
}
Here is the front-end containing the "sign up" part:
<div class="container and">
<div class="row">
<div class="col-lg-6 pull-right">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Create a new account.</h3>
</div>
<div class="panel-body">
<form id="loginform" action="includes/signup.inc.php" method="POST">
<div class="row">
<div class="form-group has-error col-md-6">
<input type="text" class="form-control" name="firstname" placeholder="First Name">
</div>
<div class="form-group has-error col-md-6">
<input type="text" class="form-control" name="surname" placeholder="Surname">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<input type="text" class="form-control" name="email" placeholder="Email Address">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12 padding-top-10">
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<input type="password" class="form-control" name="cpassword" placeholder="Confirm Password">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<h4> Birthday</h4>
<input type="date" class="form-control" name="bdate" value="birthdate">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-6 padding-top-10">
<div class="pull-right">
Male: <input type="radio" name="gender" value="male" />
</div>
</div>
<div class=" form-group has-error col-md-6 padding-top-10">
Female: <input type="radio" name="gender" value="female">
</div>
</div>
<div class="padding-top-10">
<button class="btn btn-success" name="submit">Create Account</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

You missed the button type attribute which will be type="submit" If you didn't include this type attribute, the form will not submit data.
Your code:
<button class="btn btn-success" name="submit">Create Account</button>
It should be:
<button type="submit" class="btn btn-success" name="submit">Create Account</button>
If, this is not your problem, let me know! Thanks.

Related

Not displaying the error messages in PHP

If there are any errors while registration or login into the sit then it is not displaying the error message just printing the variable.Here is the code.Tried by doing echo as well but it is also not working.Tried with storing the result in a variable and displaying the message but still not worked.
<?php
include 'includes/db.php';
$match = '';
if(isset($_POST['submit_user']))
{
$email = $_POST['email'];
$check=mysqli_query($conn,"select * from users where user_email='$email'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0)
{
$msg = "Email Already Exists";
header('Location:registration.php?msg=$msg');
}
else
{
if($_POST['password'] == $_POST['con_password'])
{
$ins_sql = "INSERT INTO users (first_name, last_name, user_email, user_password, username) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[email]', '$_POST[password]', '$_POST[username]')";
$run_sql = mysqli_query($conn,$ins_sql);
}
else
{
$match = '<div class="alert alert-danger">Password doesn&apos;t match!</div>';
}
}
}
?>
<form class="form-horizontal" action="registration.php" method="post" role="form">
<div class="body">
<div class="row clearfix">
<div class="col-sm-6 col-xs-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="First Name" name="first_name" id="first_name" onkeypress="return checkSpcialChar(event)" required>
</div>
</div>
</div>
<div class="col-sm-6 col-xs-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="Last Name" name="last_name" id="last_name" required>
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="Enter Your Email" name="email" id="email" required>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="password" class="form-control" placeholder="Password" name="password" id="password" required>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="type" class="form-control" placeholder="Confirm Password" name="con_password" id="con_password" required>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="Enter Username" name="username" id="username">
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group drop-custum">
<select class="form-control show-tick" name="user_role" id="user_role" required>
<option value="">-- Select User role --</option>
<option value="admin">Admin</option>
<option value="employee">Employee</option>
</select>
</div>
</div>
<div class="col-sm-12">
<button type="submit" class="btn btn-raised g-bg-cyan" name="submit_user" id="subject">Submit</button>
<button type="submit" class="btn btn-raised">Cancel</button>
</div>
<span>
<?php if(isset($_GET['msg']))
echo $_GET['msg'];
?>
</span>
</div>
</div>
</form>

form not responding to query

I am currently working on php and html form. I want to check if duplicate user or email is there. below is my html code.
<div class="container" align="center">
<form method="post" class="form-horizontal" action="signup.php">
<?php include('errors.php');?>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
<label class="col-sm-4 control-label">Full name</label>
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="First_Name" placeholder="First name" <?php echo $First_Name?> />
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="Last_Name" placeholder="Last name" <?php echo $Last_Name?> />
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
<label class="control-label">Username</label>
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="Username" <?php echo $Username?>/>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
<label class="control-label">Email address</label>
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="Email" <?php echo $Email?>/>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
<label class="control-label">Password</label>
</div>
<div class="col-xs-6">
<input type="password" class="form-control" name="Password_1" />
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
<label class="control-label">Confirm Password</label>
</div>
<div class="col-xs-6">
<input type="password" class="form-control" name="Password_2" />
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-info"></span>
<label class="control-label">Gender</label>
</div>
<div class="col-xs-6">
<div class="radio">
<label>
<input type="radio" name="Gender" value="male" /> Male
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="Gender" value="female" /> Female
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="Gender" value="other" /> Other
</label>
</div>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-date"></span>
<label class="control-label">Date of birth</label>
</div>
<div class="col-xs-6">
<input type="date" class="form-control" name="Birthday" placeholder="YYYY/MM/DD" <?php echo $Birthday?>/>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-home"></span>
<label class="control-label">Address</label>
</div>
<div class="col-xs-5">
<textarea placeholder="Enter Address Here.." rows="3" name="Address" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-9 col-xs-offset-3">
<button type="submit" class="btn btn-primary" name="reg_user">Submit</button>
</div>
</div>
</form>
</div>
here is my datapost file
<html>
<body>
<?php
include 'databaseconn.php';
print_r($_POST);
$First_Name = "";
$Last_Name= "";
$Username= "";
$Email= "";
$Password_1="";
$Password_2="";
$Gender= "";
$Birthday= "";
$Address= "";
$errors = array();
if (isset($_POST['reg_user']))
{
$First_Name = mysqli_real_escape_string($connect,$_POST['First_Name']);
$Last_Name= mysqli_real_escape_string($connect,$_POST['Last_Name']);
$Username= mysqli_real_escape_string($connect,$_POST['Username']);
$Email= mysqli_real_escape_string($connect,$_POST['Email']);
$Password_1=mysqli_real_escape_string($connect,$_POST['Password_1']);
$Password_2=mysqli_real_escape_string($connect,$_POST['Password_2']);
$Gender=mysqli_real_escape_string($connect,$_POST['Gender']);
$Birthday=mysqli_real_escape_string($connect,$_POST['Birthday']);
$Address=mysqli_real_escape_string($connect,$_POST['Address']);
if (empty($First_Name))
{
array_push($errors,"First name is required");
}
if (empty($Last_Name))
{
array_push($errors,"Last name is required");
}
if (empty($Username))
{
array_push($errors,"Username is required");
}
if (empty($Email))
{
array_push($errors,"Email id is required");
}
if (empty($Password_1))
{
array_push($errors,"Password is required");
}
if ($Password_1 != $Password_2)
{
array_push($errors,"Two password do not match");
}
if (empty($Gender))
{
array_push($errors,"Gender is required");
}
if (empty($Birthday))
{
array_push($errors,"Birthday is required");
}
if (empty($Address))
{
array_push($errors,"Address is required");
}
}
$user_check_query= "SELECT * FROM `user_data` WHERE Username='$Username' OR Email ='$Email' LIMIT 1";
$result = mysqli_query($connect, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user)
{
if($user['Username'] === $Username)
{
array_push($errors, "Username already exists");
}
if($user['Email'] === $Email)
{
array_push($errors, "Email already exists");
}
}
if (count($errors) == 0)
{
$Password = md5($Password_1);
mysqli_query($connect, "INSERT INTO `user_data` (`First_Name`, `Last_Name`, `Username`, `Email`, `Password`, `Gender`, `Birthday`, `Address`) VALUES ('$First_Name', '$Last_Name', '$Username', '$Email', '$Password', '$Gender', '$Birthday', '$Address')");
}
if(mysqli_affected_rows($connect)>0)
{
echo'<p> User successfully registered </p>';
echo' Go BAck ';
}
else
{
echo 'Registration not successfull';
echo 'mysqli_error($connect)';
}
?>
</body>
</html>
and my database connection file is
<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename="smartparking";
// Create connection
$connect = mysqli_connect($servername, $username, $password,$databasename);
// Check connection
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
when I run the code and enter data, it doesn't add data to the database. it is checking for username and email. but data is not inserted for the new users.

PHP form not submitting correctly

I have a form that displays user data based on a query. The form is meant to allow users to update their user account information. i.e., First name, Last name, Email address, etc.
account.php - form
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<label class="col-lg-3 control-label">First name:</label>
<div class="col-lg-8">
<input class="form-control" name="fName" type="text" placeholder="<?php echo $fName ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Last name:</label>
<div class="col-lg-8">
<input class="form-control" name="lName" type="text" placeholder="<?php echo $lName ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" name="emailAddress" type="text" placeholder="<?php echo $emailAddress ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Username:</label>
<div class="col-md-8">
<input class="form-control" name="username" type="text" placeholder="<?php echo $username ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password:</label>
<div class="col-md-8">
<input class="form-control" name="password" type="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm password:</label>
<div class="col-md-8">
<input class="form-control" name="confirmPassword" type="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Update" name="updateaccount" type="button">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
updateaccount.php
<?php
$msg = "";
if(isset($_POST["updateaccount"]))
{
$fName = $_POST["fName"];
$lName = $_POST["lName"];
$username = $_POST["username"];
$emailAddress = $_POST["emailAddress"];
$password = $_POST["password"];
$fName = mysqli_real_escape_string($db, $fName);
$lName = mysqli_real_escape_string($db, $lName);
$username = mysqli_real_escape_string($db, $username);
$emailAddress = mysqli_real_escape_string($db, $emailAddress);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql="SELECT emailAddress FROM users WHERE emailAddress='$emailAddress'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
$msg = "Sorry...This email already exists";
}
else
{
$query = mysqli_query($db, "INSERT INTO users (fName, lName, username, emailAddress, password)VALUES ('$fName', '$lName', '$username', '$emailAddress, '$password')");
if($query)
{
$msg = "Your account has been updated";
}
}
}
?>
The above code is called on the account.php page:
include ("getuseraccount.php");
Did I miss something?
You are using input type button instead of submit for the Update button. Use 'submit' type instead of 'button' type.
<input class="btn btn-primary" value="Update" name="updateaccount" type="button">
try
<input class="btn btn-primary" value="Update" name="updateaccount" type="submit">
Unless you're using Ajax to submit the form (which you made no mention of, you need to add the file that you are submitting the form information to.
<form class="form-horizontal" role="form" method="post" action="updateaccount.php">

Storing the values into database using php forms

Final Edit:
Thank you everyone for the help. I have been trying to write all the code related to connection in index.php rather than submit.php. It is resolved now.
Edit:
I have updated the code based on your feedback.
I am able to get the values to the database now but the thing is it is showing only empty results. here is the updated code.
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST)) {
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");
}
?>
Here is the snapshot of the database
I have a form made using HTML. I want to store the results when i submit the form in the database. The connection was successful but the data is not being stored in the database.
Basically what submit.php does is just sent the text "Successfully submited the form".
Here's my code:
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP code:
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
if (isset($_POST['submit'])) {
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message');");
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>
None of your input fields have a name="" attribute, including the button. So none of these fields will be sent in the $_POST array.
Add a name="" attribute like this to all the fields you want sent to PHP
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div>
<div class="col-lg-1"></div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1"></div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" name="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1"></div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn name="submit" btn-primary">Submit</button>
</div>
</div>
</form>
Also in your code in submit.php change this so you see an actual error message if one occurs.
if ($result) {
$message="successfully sent the query!!";
} else {
$message="Insert failed : " . mysqli_error($conn);
}
echo $message;
Although this does assume you are actually showing the $message value somewhere in your code that you have not shown us.
You have to add name attribute to your button element so that if (isset($_POST['submit'])) will be true.
Please change
<button type="submit" class="btn btn-primary">Submit</button>
to
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
or
<input type="submit" name="submit" value="Submit" class="btn btn-primary" />
First of all you must need to provide name attribute for each input tags and button tags for a better approach :
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="email" id ="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="subject" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" name ="message" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" name ="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Php Code for Insert data in DB :
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('".$name."', '".$email."', '".$subject."', '".$message."')");
Try this code :-
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");
echo "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";//die;
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>
<form action="index.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="name" id ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
</form>

submit form data to database table

I get problem in inserting data to database table. I have checked everything from table fields to the form fields. Everything is ok and even print_r prints the result but data is not inserted to database. it returns empty result set.
my form code
<?php include('header.php'); ?>
<div class="page container">
<div class="row col-12 register-page">
<form class="form-horizontal" role="form" action="register-process.php" method="post">
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firstname" name="firstname"
placeholder="Enter First Name">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastname" name="lastname"
placeholder="Enter Last Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="email" name="email"
placeholder="Enter Your Email">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password"
placeholder="Enter your password">
</div>
</div>
<div class="form-group">
<label for="confirm-password" class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="cpassword" name="cpassword"
placeholder="Confirm password">
</div>
</div>
<div class="form-group">
<label for="birth-year" class="col-sm-2 control-label">Birth Year</label>
<div class="col-sm-10">
<select id="year" class="birth-year" name="birth-year">
<?php
for($i = 1970; $i < date("Y")+1; $i++){
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="gender" class="col-sm-2 control-label register-gender">Gender</label>
<div class="col-sm-10">
<select id="registration_gender" class="select-register " required="required" name="gender">
<option selected="selected" value="">Gender</option>
<option value="_UE_M">Male</option>
<option value="_UE_MRS">Female</option>
</select>
</div>
</div>
<div class="form-group form-action">
<div class="form-action">
<input type="submit" name="submit" class="btn btn-large btn-primary" value="Lets Get Started" >
</div>
</div>
</form>
</div>
</div>
and my register-process code
<div class="page container">
<div class="row col-12 register-page">
<?php
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$cpassword = md5($_POST['cpassword']);
$birthyear = $_POST['birth-year'];
$gender = $_POST['gender'];
if($fname && $lname && $email && $password && $cpassword){
if($password == $cpassword){
include("config.php");
$insert = 'INSERT INTO users(firstname,lastname,email,password,birth-year,gender)
VALUES("'.$fname.'","'.$lname.'","'.$email.'","'.$password.'","'.$birthyear.'","'.$gender.'")';
mysql_query($insert);
echo "registered successfully";
}
else{
header("Location: register.php");
echo "your password do not match.";
}
}
else{
echo "complete the form please.";
header("Location: register.php");
}
?>
</div>
</div>
The line
if($fname && $lname && $email && $password && $cpassword){
is checking if these values are true, which they aren't. The PHP code around this will result in SQL injections as you're not validating the values entered in your form. Look at escaping SQL and also removing HTML entities.
Use the function isset() on your $_POST variables, this way you can validate the form was filled in correctly. Once you're happy the form is filled in correctly, SQL escape and removal of html entities into your local variables and then use these to insert into SQL.

Categories