Storing the values into database using php forms - php

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>

Related

What might the issue with this my code to upload image in my users form

I created a form to capture user details including their passport, but when ever the user fill the form, it went blank without populating the database with the filled data, but the image will be moved to specified directory. How do correct the error so that the data and the image path will be populated into the database.
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$image = $_FILES['image']['name'];
$regno=$_POST['regno'];
$email=$_POST['email'];
$password=$_POST['password'];
$status=1;
// image file directory
$target = "images/".basename($image);
move_uploaded_file($_FILES['image']['tmp_name'], $target);
$sql="INSERT INTO tblstudents(name,image,regno,email,password,status) VALUES(:name,:image,:regno,:email,:password, :status)";
$query = $dbh->prepare($sql);
$query->bindParam(':name',$name,PDO::PARAM_STR);
$query->bindParam(':image',$_image,PDO::PARAM_STR);
$query->bindParam(':regno',$regno,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':password',$password,PDO::PARAM_STR);
$query->bindParam(':status',$status,PDO::PARAM_STR);
$query->execute();
$Id = $dbh->Id();
if($Id)
{
$msg="user added successfully";
}
elseif (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else
{
$error="Something went wrong. Please try again";
}
}
?>
Here is my html form
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<!-- image file enctype="multipart/form-data" -->
<div class="form-group">
<label for="default" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="name" required="required" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="default" class="col-sm-2 control-label">Registration Number</label>
<div class="col-sm-10">
<input type="text" name="regno" class="form-control" id="regno" maxlength="50" required="required" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="default" class="col-sm-2 control-label">Email Address</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="email" required="required" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="default" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="password" required="required" autocomplete="off">
</div>
</div>
<!-- passport to upload -->
<div class="form-group">
<label for="default" class="col-sm-2 control-label">Upload Passport</label>
<div class="col-sm-10">
<input type="file" name="image" id="image">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

html5 form not submitting nor taking action to mysql table using php

php form not submitting or doing any action from the form to my database despite following all the rules for connecting the form to mysql with php
the php code supposed to get the infor from the form using the name tags but no action nor any error occurs when the data is submitted
HTML Form
<form id="contactForm" method="post" action="register.php">
<div class="row">
<div class="col-md-6 wow fadeInLeft">
<div class="input-group">
<label class="sr-only" for="name">Name</label>
<span class="input-group-addon" id="basic-addon1"><i class="fa fa-user"></i></span>
<input id="name" name="name" type="text" class="form-control" required="" placeholder="Name">
</div>
</div>
<div class="col-md-6 wow fadeInRight">
<div class="input-group">
<label class="sr-only" for="email">Email address</label>
<span class="input-group-addon" id="basic-addon2"><i class="fa fa-envelope"></i></span>
<input id="email" name="email" type="email" class="form-control" required="" placeholder="Email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 wow fadeInLeft">
<div class="input-group">
<label class="sr-only" for="phone">Phone</label>
<span class="input-group-addon" id="basic-addon3"><i class="fa fa-phone"></i></span>
<input id="phone" name="mobile" type="tel" class="form-control" placeholder="Phone">
</div>
</div>
<div class="col-md-6 wow fadeInRight">
<div class="input-group">
<label class="sr-only" for="subject">Location</label>
<span class="input-group-addon" id="basic-addon4"><i class="fa fa-file-text"></i></span>
<input id="subject" name="location" type="text" class="form-control" required="" placeholder="location">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 wow fadeInLeft">
<div class="input-group">
<label class="sr-only" for="phone">Username</label>
<span class="input-group-addon" id="basic-addon3"><i class="fa fa-phone"></i></span>
<input id="phone" name="username" type="text" class="form-control" required=""placeholder="username">
</div>
</div>
<div class="col-md-6 wow fadeInRight">
<div class="input-group">
<label class="sr-only" for="subject">Password</label>
<span class="input-group-addon" id="basic-addon4"><i class="fa fa-file-text"></i></span>
<input id="subject" name="password" type="text" class="form-control" required="" placeholder="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 wow fadeInRight">
<div class="input-group">
<label class="sr-only" for="subject">Interest</label>
<span class="input-group-addon" id="basic-addon4"><i class="fa fa-file-text"></i></span>
<input id="subject" name="interest" type="text" class="form-control" required="" placeholder="interest">
</div>
</div>
</div>
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="Register">
</form>
PHP Form
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$cusername = $_POST['username'];
$cpassword = $_POST['password'];
$location = $_POST['location'];
$interest = $_POST['interest'];
$sql = "INSERT INTO users(name,email,mobile,location,username,password,interest)
VALUES('$name','$email','$mobile','$location','$cusername','$cpassword','$interest')";
mysql_query($sql) or die(mysql_error());
session_start();
$_SESSION['username']=$cusername;
echo "<script type=\"text/javascript\">
alert(\"Registration Complete , you will be redirected shortly\");
window.location = \"profile.php\"
</script>";
}
please advice what seems to be the problem that i am missing or not seeing
try to connect with db first and pass the link into your query like this
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO users(name,email,mobile,location,username,password,interest) VALUES('$name','$email','$mobile','$location','$cusername','$cpassword','$interest')";
// here put your LINK-CONNECTION
mysql_query($sql,$link) or die(mysql_error());
mysql_close($link);
and please filter the content you get from fields in your post, to avoid sql injection and other stuff.
first of all thank you all for your support , i simply missed the ID that the form had which was connected to a JS file for submitting the data and that was why the form was not seeing the php code
once removed everything was working fine
thank you all

Cannot insert when clicking a button in Mysql using PHP

I am a beginner in web development, I used have an html elements, like textbox and other type of elements, I want to use them as my object and when they have a value and click a button, it will save the value of all the elements in mysql.
I have a code like this, but it cannot be inserted and anything does not happen when clicking a button.
Please help.
PHP:
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'ytp');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if(!$db ) {
die('Could not connect: ' . mysqli_error());
}
$fname = "";
$mname = "";
$lname = "";
$funame = "";
$cnum = "";
$bday = "";
$age = "";
$add = "";
if (isset($_POST['submit'])){
$fname = $_POST['fName'];
$mname = $_POST['mName'];
$lname = $_POST['lName'];
$funame = $_POST['fuName'];
$cnum = $_POST['Cnumber'];
$bday = $_POST['bday'];
$age = $_POST['age'];
$add = $_POST["address"];
}
$sql = "INSERT INTO employee ".
"(fName,mName,lName,fuName,cNumber,bDay,Age,Address) ".
"VALUES ('$fname','$mname','$lname','$funame','$cnum','$bday','$age','$add' )";
if (! mysqli_query($db , $sql)){
echo 'Cannot Insert';
}
else{
echo 'Success';
}
?>
HTML:
<div class="content">
<div class="row">
<div class="col-md-10">
<div class="card">
<div class="card-header">
<h5 class="title">Add User Information</h5>
</div>
<div class="card-body">
<form method="POST" action="php_functions\saveEmployee.php" name="INSERT">
<div class="row">
<div class="col-md-5 pr-md-1">
<div class="form-group">
<label>Company (disabled)</label>
<input type="text" class="form-control" disabled="" placeholder="Company" value="Benchmark Valuer's Inc.">
</div>
</div>
<div class="col-md-3 px-md-1">
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" placeholder="User ID" value="" id="id" name ="id" disabled>
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" placeholder="s.sample#gmail.com" id="email" name ="email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" placeholder="First Name" id="fName" name ="fName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Middle Name</label>
<input type="text" class="form-control" placeholder="Middle Name" id="mName" name ="mName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" id="lName" name ="lName">
</div>
</div>
<div class="col-md-4 pl-md-1" hidden>
<div class="form-group">
<label>Fullname</label>
<input type="text" class="form-control" placeholder="Full Name" id="fuName" name ="fuName">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Contact Number</label>
<input type="tel" class="form-control" placeholder="Contact Number" id="Cnumber" name="Cnumber">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Age</label>
<input type="number" class="form-control" placeholder="Age" id="age" name="age">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Birthday</label>
<input type="date" class="form-control" placeholder="Birthday" id="bday" name="bday">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" placeholder="Home Address" id="address" name ="address">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="Username" id="uName">
</div>
</div>
<div class="col-md-4 px-md-1">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" id="pWord">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>UserType</label>
<input type="number" class="form-control" placeholder="0" id="uType">
</div>
</div>
</div>
<div class="row" hidden>
<div class="col-md-12">
<div class="form-group">
<label>Image Path:</label>
<input type="text" class="form-control" placeholder="C:\\" id="imageURL">
</div>
</div>
</div>
</form>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-fill btn-primary" id="submit" name="submit">Save</button>
<button class="btn btn-fill btn-success" id="btnBrowse">Browse</button>
<button class="btn btn-fill btn-danger" id="btnCancel">Cancel</button>
</div>
</div>
</div>
</div>
</div>
Your HTML form is closing before submit button. You should close that after submit button and also need to manage hierarchy of opening form tag as below:
<form method="POST" action="php_functions\saveEmployee.php" name="INSERT">
<div class="card-body">
<div class="row">
<div class="col-md-5 pr-md-1">
<div class="form-group">
<label>Company (disabled)</label>
<input type="text" class="form-control" disabled="" placeholder="Company" value="Benchmark Valuer's Inc.">
</div>
</div>
<div class="col-md-3 px-md-1">
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" placeholder="User ID" value="" id="id" name ="id" disabled>
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" placeholder="s.sample#gmail.com" id="email" name ="email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" placeholder="First Name" id="fName" name ="fName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Middle Name</label>
<input type="text" class="form-control" placeholder="Middle Name" id="mName" name ="mName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" id="lName" name ="lName">
</div>
</div>
<div class="col-md-4 pl-md-1" hidden>
<div class="form-group">
<label>Fullname</label>
<input type="text" class="form-control" placeholder="Full Name" id="fuName" name ="fuName">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Contact Number</label>
<input type="tel" class="form-control" placeholder="Contact Number" id="Cnumber" name="Cnumber">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Age</label>
<input type="number" class="form-control" placeholder="Age" id="age" name="age">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Birthday</label>
<input type="date" class="form-control" placeholder="Birthday" id="bday" name="bday">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" placeholder="Home Address" id="address" name ="address">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="Username" id="uName">
</div>
</div>
<div class="col-md-4 px-md-1">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" id="pWord">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>UserType</label>
<input type="number" class="form-control" placeholder="0" id="uType">
</div>
</div>
</div>
<div class="row" hidden>
<div class="col-md-12">
<div class="form-group">
<label>Image Path:</label>
<input type="text" class="form-control" placeholder="C:\\" id="imageURL">
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-fill btn-primary" id="submit" name="submit">Save</button>
<button class="btn btn-fill btn-success" id="btnBrowse">Browse</button>
<button class="btn btn-fill btn-danger" id="btnCancel">Cancel</button>
</div>
</form>
Hope it helps you.
the form has been closed before the submit button.
the </form> tag should be placed after the <div class="card-footer">...</div>.
please try it. hope it will help.

Inserting data from a form into SQLite3 database using PHP

I am trying to create a registration from and then post the data into a SQlite databases file.
My form looks like this:
<form action="registerprocess_test.php" class="form-horizontal" id=
"register_form" method="post" name="register_form" role="form">
<h2>Registration Form</h2>
<div class="form-group">
<label class="col-sm-3 control-label" for="firstname">First
Name</label>
<div class="col-sm-6">
<input autofocus="" class="form-control" id="firstname" name=
"firstname" placeholder="First Name" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="lastname">Last
Name</label>
<div class="col-sm-6">
<input autofocus="" class="form-control" id="lastname" name=
"lastname" placeholder="Last Name" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="email">Username</label>
<div class="col-sm-6">
<input class="form-control" id="username" name="username"
placeholder="Username" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for=
"password">Password</label>
<div class="col-sm-6">
<input class="form-control" id="password" name="password"
placeholder="Password" type="password">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="country">Country</label>
<div class="col-sm-6">
<select class="form-control" id="country" name="country">
<option>
United Kingdom
</option>
<option>
United States
</option>
</select>
</div>
</div><!-- /.form-group -->
<div class="form-group">
<label class="control-label col-sm-3">Gender</label>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-4">
<label class="radio-inline"><input id="femaleRadio"
type="radio" value="Female">Female</label>
</div>
<div class="col-sm-4">
<label class="radio-inline"><input id="maleRadio" name=
"gender" type="radio" value="Male">Male</label>
</div>
</div>
</div>
</div><!-- /.form-group -->
<div class="form-group">
<div class="col-sm-6">
<div class="checkbox"></div>
</div><!-- /.form-group -->
<div class="form-group">
<div class="col-sm-6 col-sm-offset-3">
<div class="checkbox">
<label><input type="checkbox">I accept <a href=
"#">Terms & Conditions</a></label>
</div>
</div>
</div><!-- /.form-group -->
<div class="form-group">
<div class="col-sm-6 col-sm-offset-3">
<button class="btn btn-primary btn-block" type=
"submit">Register</button>
</div>
</div>
</div>
</form>
Then my PHP looks like this:
<?php
try
{
//open the database
$db = new PDO('sqlite:users.db');
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$username = $_POST["username"];
$password = $_POST["password"];
$gender = $_POST["gender"];
$country = $_POST["country"];
//Insert record
$db->exec("INSERT INTO registered_users (firstname, lastname, username, password, gender, country) VALUES ('$firstname', '$lastname', '$username', '$password', '$gender', $country);");
//now output the data to a simple html table...
print "<table border=1>";
print "<tr><td>firstname</td><td>lastname</td><td>username</td><td>password</td><td>gender</td><td>country</td></tr>";
$result = $db->query('SELECT * FROM registered_users');
foreach($result as $row)
{
print "<tr><td>".$row['firstname']."</td>";
print "<td>".$row['lastname']."</td>";
print "<td>".$row['username']."</td>";
print "<td>".$row['password']."</td>";
print "<td>".$row['gender']."</td>";
print "<td>".$row['country']."</td>";
}
print "</table>";
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : ' .$e->getMessage();
}
?>
I know I am connecting to the database as it displays the current data in the database in the table, upon clicking submit. However it does not insert the data from the registration form. Am I missing something important in my php code?
I did some error checking, It was because I had another column within my table that I was not inserting into. So I deleted this column from the database and now it works fine.

Submitting HTML data to a google App Engine MySQL database using PHP

I have been trying to create a simple web form that will be used to submit data to a Google App Engine database that uses MySQL. The database is connecting fine and the try catch statement seems to be working properly. The problem is that when i click the submit button, the data is not being committed into the database. Any help would be much appreciated, I'm sure there is just some small error that i am overlooking.
HTML form
<form class="form-horizontal" role="form" action="connection.php" method="post">
<div class="form-group">
<label for="patientName" class="col-sm-2 control-label">Patient Name</label>
<div class="col-sm-10">
<input type="Name" class="form-control" id="patientName" name="patientName">
</div>
</div>
<div class="form-group">
<label for="Address1" class="col-sm-2 control-label">Address 1</label>
<div class="col-sm-10">
<input type="text" class="form-control" rows='3' id="Address1" name="Address1"></input>
</div>
</div>
<div class="form-group">
<label for="Address2" class="col-sm-2 control-label">Address 2</label>
<div class="col-sm-10">
<input type="text" class="form-control" rows='3' id="Address2" name="Address2"></input>
</div>
</div>
<div class="form-group">
<label for="Address3" class="col-sm-2 control-label">Address 3</label>
<div class="col-sm-10">
<input type="text" class="form-control" rows='3' id="Address3" name="Address3"></input>
</div>
</div>
<div class="form-group">
<label for="postCode" class="col-sm-2 control-label">PostCode</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="postCode" name="postCode">
</div>
</div>
<div class="form-group">
<label for="symptoms" class="col-sm-2 control-label">Symptoms</label>
<div class="col-sm-10">
<textarea type="text" class="form-control" rows='5' id="symptoms" name="symptoms"></textarea>
</div>
</div>
<div class="form-group">
<label for="contactNumber" class="col-sm-2 control-label">Contact Number</label>
<div class="col-sm-10">
<input type="Tel" class="form-control" id="contactNumber" name="contactNumber">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Log Call Details</button>
</div>
</div>
</form>
PHP connection file
<?php
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
try {
$db = new pdo('mysql:host=111.111.111.11:3306;dbname=MyDB',
'root',
'password'
);
$patientName = $_POST["patientName"];
$address1 = $_POST["Address1"];
$address2 = $_POST["Address2"];
$address3 = $_POST["Address3"];
$postCode = $_POST["postCode"];
$symptoms = $_POST["symptoms"];
$contactNumber = $_POST["contactNumber"];
$sql = "INSERT INTO patient (patientName, patientAddress1, patientAddress2, patientAddress3, patientPostcode, PatientSymptoms, patientPhoneNumber)
VALUES ($patientName, $address1, $address2, $address3, $postCode, $symptoms, $contactNumber)";
$db->execute($sql);
} catch (PDOException $ex) {
echo "Could not connect to the database.";
exit;
}
$db = null;
echo "Woo-hoo!";
?>
I should probably also mention that the HTML page is using bootstrap.
You can try this code snippet below, It works, I did not used $_POST data but hard-coded values, Hope it helps !
more infos:
http://php.net/manual/en/pdo.prepare.php
$username="test12";
$role="test1";
$sql = "INSERT INTO test (username, role) VALUES (:username, :role)";
$conn = new pdo('mysql:host=127.0.0.1:3306;dbname=home',
'root',
'password'
);
$q = $conn->prepare($sql);
$q->execute(array(':username'=>$username, ':role'=>$role));

Categories