PHP MySQL Insert into table [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've followed a tutorial tot he exact code and i can't seem to get this to work
When I submit the form nothing gets put into my table
I can't seem to find the problem, ive changed a bunch of stuff but i cant seem to find the answer.
If anyone can have a look at my code and help me resolve the issue i would be very thankfull
Code
<form class="form" method="post" action="index.php">
<h2>
Post new account...
</h2>
<div class="form-group">
<label for="title">Title</label>
<input required type="text" class="form-control" name="title" placeholder="Enter a title for the account...">
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="server">Server</label>
<input required type="text" class="form-control" name="server" placeholder="Enter Server...">
</div>
<div class="col-md-6">
<label for="price">Price</label>
<input required type="number" class="form-control" name="price" placeholder="Enter Price...">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="Energy">Energy</label>
<input required type="number" class="form-control" name="energy" placeholder="Enter Energy...">
</div>
<div class="col-md-6">
<label for="Stamina">Stamina</label>
<input required type="number" class="form-control" name="stamina" placeholder="Enter Stamina...">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="Jewels">Jewels</label>
<input required type="number" class="form-control" name="jewels" placeholder="Enter Jewels...">
</div>
<div class="col-md-6">
<label for="honor">Stamina</label>
<input required type="number" class="form-control" name="honor" placeholder="Enter Honor...">
</div>
</div>
</div>
<div class="form-group">
<label for="image">Image URL</label>
<input required type="text" class="form-control" name="image" placeholder="Enter the image link...">
</div>
<div class="form-group">
<label for="link">Link URL</label>
<input required type="text" class="form-control" name="link" placeholder="Enter the payment link...">
</div>
<div class="form-group">
<input class="btn btn-primary btn-md" type="submit" name="submit">
</div>
</form>
<?php
if (isset($_POST['submit'])) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("We're having problems at the moment, please come back later!");
}
$query = "INSERT INTO accounts (title,price,energy,stamina,jewels,honor,damage,server,image,link) VALUES ('$_POST[title]','$_POST[price]','$_POST[energy]','$_POST[stamina]','$_POST[jewels]','$_POST[honor]','$_POST[damage]','$_POST[server]','$_POST[image]','$_POST[link]')";
mysql_query($query,$conn);
mysql_close($conn);
};
?>

Check your connection:
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Then check if it is inserting into the database or not. If there's an error, it will let you know as well.
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Also, don't mix mysql_ with mysqli_. Start using mysqli_ instead of mysql_. mysql_ is deprecated.

Related

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.

How to implement form inside a tab panel?

I have written a php code for a form which is inside a tab panel. But the values are not getting inside database. I have written the php code on the top of the php file.After submitting the form submit it goes to the second tab when the php code is written on top of the file before tag and the values obtained from the form is not getting inserted into the database. Why is it like that ? I dont know how to correct this. Can anyone help me on this ?
php code :
<?php
include("database.php");
if(isset($_REQUEST['save'])){
if($_POST['start']!=''&&$_POST['end']!=''&&$_POST['firstname']!=''&&$_POST['lastname']!=''&&$_POST['age']!='')
{
$start = $_POST['start'];
$end = $_POST['end'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$cur = date("d.m.Y");
$j = 'CUS'.rand(1,1000000);
$sql = "insert into tbl_customer(id,customer_id,firstname,lastname,age,rentalfromdate,rentaltodate,date) values(' ','$j','$firstname','$lastname','$age','$start','$end','$cur')";
mysqli_query($con,$sql) or die(mysqli_error($con));
}
}
?>
tab :
<div class="tab-pane active" role="tabpanel" id="step1">
<div class="step1">
<div class="spec">
<div class="col-md-6">
</div>
<div class="col-md-6">
</div>
</div>
<div class="main_cont">
<div class="main-agileits">
<div class="register-box">
<form name="customer" id="customer" method="post" action="#step1">
<div id="flight-datepicker" class="input-daterange input-group">
<div class="col-md-6 s_pad">
<label>First day of rental</label><span class="fontawesome-calendar"></span>
<input type="text" id="start-date" name="start" placeholder="Select Date" data-date-format="DD, MM d" class="input-sm form-control" /><span class="date-text date-depart"></span>
</div>
<div class="col-md-6 s_pad">
<label>Last day of rental</label><span class="fontawesome-calendar"></span>
<input type="text" id="end-date" name="end" placeholder="Select Date" data-date-format="DD, MM d" class="input-sm form-control"/><span class="date-text date-return"></span>
</div>
</div>
<div>
<div class="form-group col-md-12 s_pad">
<label for="exampleInputEmail1">First Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="firstname" placeholder="First Name" />
</div>
<div class="form-group col-md-12 s_pad">
<label for="exampleInputEmail1">Last Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="lastname" placeholder="Last Name" />
</div>
<div class="form-group col-md-6 s_pad">
<label for="exampleInputEmail1" class="ctred">Age only if under 18</label>
<input type="text" class="form-control" name="age" id="exampleInputName" placeholder="Age">
</div>
</div>
<div>
<button type="submit" name="save" class="btn btn-primary mag_top2 next-step">Continue</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Do like this. Then you will get the error or success message:-
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
I hope id field is primary key so you dont require it in your sql query so try the following query.
$sql = "insert into tbl_customer(customer_id,firstname,lastname,age,rentalfromdate,rentaltodate,date) values('$j','$firstname','$lastname','$age','$start','$end','$cur')";

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 button will not send form data to MySQL database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a problem that might be a syntax problem but I can't seem to figure out what I am doing wrong.
I have created a form and when I click on submit, the data in the form is not sent to my mysql database.
Here is my html code
<div class="content-wrapper">
<div class="container">
<div class="row">
<div class="col-md-10">
<h1 class="page-head-line">Forms </h1>
</div>
</div>
<div class="row">
<div class="col-md-10">
<div class="panel panel-default">
<div class="panel-heading">
BASIC FORM ELEMENTS
</div>
<div class="panel-body">
<form method="post" action="insert.php" >
<div class="form-group">
<label for="name">Name</label>
<input name="name' type="text" class="form-control" id="name" placeholder="Enter your name" required/>
</div>
<div class="form-group">
<label for="project_num">OIT-GIS Project Number</label>
<input name="project_num' type="text" class="form-control" id="project_num" placeholder="OIT-GIS Project Number" />
</div>
<div class="form-group">
<label for="project_name">Project Name</label>
<input name="name' type="text" class="form-control" id="project_name" placeholder="Project Name" required/>
</div>
<div class="form-group">
<label for="easyvista">EasyVista Ticket Number</label>
<input name="easyvista' type="text" class="form-control" id="easyvista" placeholder="EasyVista Ticket Number" />
</div>
<div class="form-group">
<label for="agency">Requestor/Agency</label>
<input name="agency' type="text" class="form-control" id="agency" placeholder="Requestor or Agency" />
</div>
<div class="form-group">
<label for="description">Description of Work:</label>
<input name="description' type="text" class="form-control" id="agency" placeholder="Description" />
</div>
<div class="form-group">
<label for="input-date">Enter Today Date</label>
<input name="input-date' type="date" value="">
<span class="result"></span>
</div>
<div class="form-group">
<div class="col-md-10">
<input id="submit" name="submit" type="submit" class="btn btn-primary">
</div>
</div>
</form>
</div>
</div>
and here is my php
<?php
echo $POST;
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("../includes/config.php");
if (isset($_POST['submit'])) {
echo $_POST['submit'];
$name = $_POST['name'];
$projectnum = $_POST['project_num'];
$projectname = $_POST['project_name'];
$easyvista = $_POST['easyvista'];
$agency = $_POST['agency'];
$description = $_POST['description'];
$startDate = $_POST['input-date'];
$sql="INSERT INTO statusreport(name, project_num, project_name, easyvista, agency, description)
VALUES
('$name','$projectnum', '$projectname', '$easyvista', '$agency', '$description')";
if (!mysqli_query($conn, $sql))
{
die('Error: ' . mysqli_connect_error($conn));
}
echo "Entry is recored <br/>";
echo "Name:", $name, "<br/>";
echo "test..................<br/>", $name;
/*header("location: http://10.1.7.129//gisadmin/admin/forms.php");*/
//echo "<script>setTimeout(\"location.href = 'http://10.1.7.129//gisadmin/admin/forms.php';\",700);</script>";
mysqli_query($conn, $sql);
}
else {
echo "No data";
}
?>
Any help would be greatly appreciated.
Thanks
You have a mixing of single and double quotes here, the name attributes are opening the value with double quotes and closing with single quotes, should be as follows:
<form method="post" action="insert.php" >
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Enter your name" required/>
</div>
<div class="form-group">
<label for="project_num">OIT-GIS Project Number</label>
<input name="project_num" type="text" class="form-control" id="project_num" placeholder="OIT-GIS Project Number" />
</div>
<div class="form-group">
<label for="project_name">Project Name</label>
<input name="project_name" type="text" class="form-control" id="project_name" placeholder="Project Name" required/>
</div>
<div class="form-group">
<label for="easyvista">EasyVista Ticket Number</label>
<input name="easyvista" type="text" class="form-control" id="easyvista" placeholder="EasyVista Ticket Number" />
</div>
<div class="form-group">
<label for="agency">Requestor/Agency</label>
<input name="agency" type="text" class="form-control" id="agency" placeholder="Requestor or Agency" />
</div>
<div class="form-group">
<label for="description">Description of Work:</label>
<input name="description" type="text" class="form-control" id="agency" placeholder="Description" />
</div>
<div class="form-group">
<label for="input-date">Enter Today Date</label>
<input name="input-date" type="date" value="">
<span class="result"></span>
</div>
<div class="form-group">
<div class="col-md-10">
<input id="submit" name="submit" type="submit" class="btn btn-primary">
</div>
</div>
</form>
And then, as #Fred -ii stated in his comment, the php script is wrong:
echo $POST;
That line is wrong, there is no variable with name $POST before that code.
Are you getting success message but database is not getting updated OR Getting Total Error...???
1) Check for double quotes and single quotes..
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Enter your name" required/>
</div>
2) Also check for path... for
include("../includes/config.php");
Is it correct or not...?
3)
<label for="project_name">Project Name</label>
<input name="name' type="text"
SHOULD BE
<label for="project_name">Project Name</label>
<input name="project_name" type="text"

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