PHP code isn't submitting into mysql database - php

I'm trying to create a login for a website. So far whenever I post to my database doesn't show the submitted information, the only things which are posted is a hashed password.
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
To submit into this php block
<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "DBNAME";
$email = NULL;
$user = NULL;
$pass1 = NULL;
if (isset($_POST['email'])){
$email = $_POST['email'];
}
if (isset($_POST['username'])){
$user = $_POST['username'];
}
if (isset($_POST['password'])){
$pass1 = $_POST['password'];
}
$hash = password_hash($pass1, PASSWORD_BCRYPT);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Users (email, username, password )
VALUES ('$email', '$user', '$hash')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Your form fields lack name attributes. Without them no values are sent your your script. This is easily testable by doing var_export($_POST).
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" name="pwd2" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
FYI, you are wide open to SQL injections

Related

Submit button in a form not sending data?

I want to send my html form data to my database which I have created using XAMPP. But, for some reason, the submit button is not working.
This is my html code:-
<form id="contact" action="contact.php" method="post" class="php-email-form">
<div class="row">
<fieldset class="col-md-6 form-group">
<input type="text" name="name2" class="form-control" id="name" placeholder="Your Name" required>
</fieldset>
<fieldset class="col-md-6 form-group mt-3 mt-md-0">
<input type="email" class="form-control" name="email2" id="email" placeholder="Your Email" required>
</fieldset>
</div>
<fieldset class="form-group mt-3">
<input type="text" class="form-control" name="phone2" id="phone" placeholder="Your Phone Number" required>
</fieldset>
<fieldset class="form-group mt-3">
<input type="text" class="form-control" name="subject2" id="subject" placeholder="Subject" required>
</fieldset>
<fieldset class="form-group mt-3">
<textarea class="form-control" name="message2" rows="5" placeholder="Message" required></textarea>
</fieldset>
<fieldset class="text-center">
<button type="submit" name="submit" id="contact-submit">Send Message</button>
</fieldset>
</form>
This is my contact.php code:-
<?php
$name = $_POST['name2'];
$fromEmail = $_POST['email2'];
$phone = $_POST['phone2'];
$subject = $_POST['subject2'];
$message = $_POST['message2'];
//Database Connection
$conn = new mysqli('localhost','root','','contactus');
if ($conn->connect_error) {
die('Connection Failed : '.$conn->connect_error);
} else {
$stmt = $conn->prepare("insert into contact(Name, Email, Phone No., Subject, Message)
values(?,?,?,?,?)");
$stmt->bind_param("ssiss",$name,$fromEmail,$phone,$subject,$message);
$execval = $stmt->execute();
echo $execval;
echo "Sent Successfully";
$stmt->close();
$conn->close();
}
?>
In order for the system to properly process the data field Phone No. (which contains a space character) in db operation, Please enclose it by backticks in your insert query prepared statement
Change
$stmt = $conn->prepare("insert into contact(Name, Email, Phone No., Subject, Message) .....
to
$stmt = $conn->prepare("insert into contact(Name, Email, `Phone No.`, Subject, Message) .....
Your submit button ID is different than what is identified in the form.
<form id="contact" action="contact.php" method="post" class="php-email-form">
<button type="submit" name="submit" id="contact">Send Message</button>

Php form not submitting, only get blank page using PDO prepared statement

-php, PDO, prepared statement, html, bootstrap, mysql database-
whenever i hit submit button i get blank php page i.e:profile.php.i see no error or records inserted successfully. please can someone help me debug this code-----------------------------------------------------------------------.
<form action="profile.php" method="GET"> <!--enctype="multipart/form-data"-->
<h1 >Profile </h1>
<div class="form-row">
<div class="form-group col-md-4">
<label>Name :</label>
<input type="text" class="form-control" required="required" name="fname" />
</div>
<div class="form-group col-md-4">
<label>Age :</label>
<input type="number" class="form-control" name="age" />
</div>
<div class="form-group col-md-4">
<label>Gender :</label>
<select class="form-control" name="gender">
<option value="">Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<div class="form-group col-md-6">
<label>Email :</label>
<input type="email" class="form-control" name="email" />
</div>
<div class="form-group col-md-6">
<label>Phone No :</label>
<input type="tel" class="form-control" required="" pattern="[0-9]{10}" name="phone" />
</div>
<div class="form-group col-md-6">
<label>Address :</label>
<textarea type="text" class="form-control" name="address"> </textarea>
</div>
<div class="form-group col-md-6">
<label>Comments :</label>
<textarea type="text" class="form-control" name="comments"> </textarea>
</div>
<div class="form-group col-md-6">
<label>Favorite Color :</label>
<br>
<input type="checkbox" value="red" name="color[]" />Red
<input type="checkbox" value="green" name="color[]" />Green
<input type="checkbox" value="blue" name="color[]" />Blue
<input type="checkbox" value="yellow" name="color[]" />Yellow
</div>
<!-- <div class="form-group col-md-6">
<label>Photo :</label>
<input type="file" name="photo" />
</div>-->
<div class="form-group col-md-6">
<input type="submit" name="submit"/>
</div>
</div>
</form>
this database connection code in dbconn.php and php code in profile.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pritesh";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
//$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST["submit"])){
include("dbconn.php");
$fname=$_REQUEST["fname"];
$age=$_REQUEST["age"];
$email=$_REQUEST["email"];
$phone=$_REQUEST["phone"];
$address=$_REQUEST["address"];
$gender=$_REQUEST["gender"];
$color=$_REQUEST["color"];
$comments=$_REQUEST["comments"];
$sql = "insert into profile (name, age, email, phone, address, gender, color, comments) values (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param($fname, $age ,$email, $phone, $address, $gender, implode(",",$color), $comments);
$stmt->execute();
$stmt->close();
}
?>
One possible problem is that you are calling accessing $conn without first checking whether it has been correctly created. This is a bad programming practice and can lead to problems. You should add an if clause to check what $conn is before you assume it's an object you can use.
Second similar assumption you make is call
implode(",",$color)
$color being a variable received via $_REQUEST, yet you assume it is an array. Making assumptions such as this, especially from data which comes from a form submission is also a bad habit.
I would start debugging this issue by adding a check to see what $conn is before using it.

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

I want to save the input select option in the database table using php

I want to save the input select option in the database table using php. I want to save it in a table with existing attributes
I know how to add it when using alone table for it but I want to add it in existing table with few other attributes
ps first question bear me
<?php
if(isset($_POST['signup'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$department = $_POST['deptlist'];
echo $name;
$connection = mysqli_connect('localhost', 'root', '', 'filetracking');
if($connection) {
echo "We are connected";
} else {
die("Database connection failed");
}
$query = "INSERT INTO faculty(name,email,contactno,password,office) ";
$query .= "VALUES ('$name', '$email', '$phone', '$password', '$department')";
$result = mysqli_query($connection, $query);
if(!$result) {
die('Query FAILED' . mysqli_error($connection));
} else {
echo "Record Create";
}
}
?>
<form method="post" action="signup.php">
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input type="text" name="name" id="Name" class="inputmd textinput form-control required" placeholder="Full Name" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="email" name="email" id="Email" class="inputmd textinput form-control required" placeholder="Email" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="phone" class="control-label">Phone</label>
<input type="number" name="phone" id="Phone" class="inputmd textinput form-control required" placeholder="Phone No" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="password" class="control-label">Password</label>
<input type="password" name="password" id="Password" class="inputmd textinput form-control" placeholder="Password" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="deptlist" class="control-label">Department</label>
<select name="deptlist" form="deptform" class="inputmd form-control" style="margin-bottom: 10px">
<option value="DCSE">Department of Computer Systems Engineering</option>
<option value="DME">Department of Mechanical Engineering</option>
<option value="DEE">Department of Electrical Engineering</option>
<option value="DCS">Department of Computer Science</option>
</select>
<div class="form-group">
<input type="submit" name="signup" id="Singup" value="Sign Up" class="btn btn-info col-md-4 col-md-offset-4">
</div>
</div>
You've mistakenly referred to a form named deptform <select name="deptlist" form="deptform" but there is no such form. This prevents the script from receiving this data.

How to show popup on same page after data submission

I have this html form
<form class="form" method="post" action="data.php">
<div class="form-group">
<input class="form-control input-lg" type="text" placeholder="Full Name" name="name" id="name" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="tel" placeholder="Phone Number" name="phone" id="phone" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="email" placeholder="Email ID" name="email" id="email" required="">
</div>
<div class="form-group">
<!--<input class="form-control input-lg option" type="date" name="date" value="Date" id="date" required="">-->
<input type="text" class="form-control input-lg option" name="date" Placeholder="Date" id="datepicker">
</div>
<div class="form-group">
<select class="form-control input-lg option" name="time" id="time" style="padding-left: 10px;color:darkgray;">
<option>Time Slot</option>
<option>10:00-11:00</option>
<option>11:00-12:00</option>
<option>12:00-13:00</option>
<option>13:00-14:00</option>
</select>
</div>
<input class="btn btn-success btn-lg" style="margin-left: 30%; margin-top:10px; padding: 5px 16px;" type="submit" value="BOOK ">
Here is code of data.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "xxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO data (name,phone,email,date,time)
VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[date]', '$_POST[time]')";
if (mysqli_query($conn, $sql)) {
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
This code shows a pop up on same window with ugly format and after click on ok it shows blank page with ..../data.php url. I want to redirect on same page.
And can i change popup style. I want to show popup in mid of the page.
Please help to solve these issues.. I am newbie
You cannot change the location of the alert-popup. You need to open a popup via window.open.
Use this below updated code to redirect back to your required page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "xxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO data (name,phone,email,date,time)
VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[date]', '$_POST[time]')";
if (mysqli_query($conn, $sql))
{
header('Location:index.php?success=1');
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Replace yourpage.php in header function with your actual name of the page.
Rename your index.html to index.php and make these changes to the index file.
<?php
$get = (isset($_GET['success'])) ? $_GET['success'] : '';
if((!empty($get)) && ($get == 1))
{
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
?>
<form class="form" method="post" action="data.php">
<div class="form-group">
<input class="form-control input-lg" type="text" placeholder="Full Name" name="name" id="name" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="tel" placeholder="Phone Number" name="phone" id="phone" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="email" placeholder="Email ID" name="email" id="email" required="">
</div>
<div class="form-group">
<!--<input class="form-control input-lg option" type="date" name="date" value="Date" id="date" required="">-->
<input type="text" class="form-control input-lg option" name="date" Placeholder="Date" id="datepicker">
</div>
<div class="form-group">
<select class="form-control input-lg option" name="time" id="time" style="padding-left: 10px;color:darkgray;">
<option>Time Slot</option>
<option>10:00-11:00</option>
<option>11:00-12:00</option>
<option>12:00-13:00</option>
<option>13:00-14:00</option>
</select>
</div>
<input class="btn btn-success btn-lg" style="margin-left: 30%; margin-top:10px; padding: 5px 16px;" type="submit" value="BOOK ">
I have also modified the data.php file again, so be sure to use the updated code.

Categories