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')";
Related
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
i am having a problem in sending mail to my server. i dont know why the problem is occurring? everything is done perfect but it is not working maybe the fields are not filling properly and alert message is also not shown
if(isset($_POST['uname']) && isset($_POST['fname']) && isset($_POST['nic']) && isset($_POST['pecno'])&& isset($_POST['quality[25]'])&& isset($_POST['bday'])&& isset($_POST['quality[26]'])&& isset($_POST['postal'])&& isset($_POST['cell'])&& isset($_POST['houseno'])&& isset($_POST['mail'])&& isset($_POST['city'])&& isset($_POST['province'])&& isset($_POST['country']) )
{
$_Name = $_POST['uname'];
$_Fname = $_POST['fname'];
$_NIC = $_POST['nic'];
$_Pecno = $_POST['pecno'];
$_Gender = $_POST['quality[25]'];
$_Bday = $_POST['bday'];
$_Qualification = $_POST['quality[26]'];
$_Postal = $_POST['postal'];
$_Cell = $_POST['cell'];
$_Houseno = $_POST['houseno'];
$_Email = $_POST['mail'];
$_City = $_POST['city'];
$_Province = $_POST['province'];
$_Country = $_POST['country'];
if(!empty($_Name) && !empty($_Fname) && !empty($_NIC) && !empty($_Pecno)&& !empty($_Gender)&& !empty($_Bday)&& !empty($_Qualification)&& !empty($_Postal)&& !empty($_Cell)&& !empty($_Houseno)&& !empty($_Email)&& !empty($_City)&& !empty($_Province)&& !empty($_Country))
{
$to = 'info#mymail.com';
$subject = 'Join us mail';
$body = 'Sender Name : '.$_Name."\n".'Sender Father Name : '.$_Fname."\n".'Sender NIC : '.$_NIC."\n".'Sender PEC Number : '.$_Pecno."\n".'Sender Gender : '.$_Gender."\n".'Sender Birthday : '.$_Bday."\n".'Sender Qualification : '.$_Qualification."\n".'Sender Postal Address : '.$_Postal."\n".'Sender Cell Number : '.$_Cell."\n".'Sender House Number : '.$_Houseno."\n".'Sender Email : '.$_Email."\n".'Sender City : '.$_City."\n".'Sender Province : '.$_Province."\n".'Sender Country : '.$_Country;
$header = 'From : '.$_Email;
if(#mail($to, $subject, $body, $header))
{
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
}else
{
echo 'Please Try again in a few mints !';
}
}
}
html code
<form method="POST" action="index.php">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="uname" name="uname" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="fname" name="fname" placeholder="Father Name" type="text" required>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="nic" name="nic" placeholder="CNIC/Passport Number" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="pecno" name="pecno" placeholder="PEC Number" type="text">
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="designation" name="designation" placeholder="Designation" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="organization" name="organization" placeholder="Organization" type="text">
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
Gender: <div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" id="quality[25]" name="quality[25]" value="1" /> Male
</label>
<label class="btn btn-default">
<input type="radio" id="quality[25]" name="quality[25]" value="2" /> Female
</label>
</div>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" type="text" onfocus="(this.type='date')" id="bday" name="bday" placeholder="Date of Birth" ><br>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
Qualification: <div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" id="quality[26]" name="quality[26]" value="1" /> Bachelors In Engineering
</label>
<label class="btn btn-default">
<input type="radio" id="quality[26]" name="quality[26]" value="2" /> Masters
</label>
<label class="btn btn-default">
<input type="radio" id="quality[26]" name="quality[26]" value="3" /> PhD
</label>
</div>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="postal" name="postal" placeholder="Postal Address" type="text" >
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control slideanim" id="cell" name="cell" placeholder="Cell Number" type="tel" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="houseno" name="houseno" placeholder="House Number" type="tel" required>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control slideanim" id="mail" name="mail" placeholder="Email Address" type="email" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="city" name="city" placeholder="City" type="text" required>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="province" name="province" placeholder="Province/State" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="country" name="country" placeholder="Country" type="text" required>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right " type="submit">Send</button>
</div>
</div>
</div>
replace #mail with mail
if(#mail($to, $subject, $body, $header))
enable error reporting
error_reporting(E_ALL);
Show last error with:
print_r(error_get_last());
and check php_error.log for a problem
phpinfo();
for checking if mail module is enabled in php.
If all pass, check /var/log/mail about the messages, here could be 1001 issues with network, server, firewall etc...
hope it helps.
As you mentioned that same code is working perfectly on other page so please check it by removing "#" from mail as adding this will hide all notices and it will display error as well. Also try to print mail body text before mail function.
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>
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"
I am trying to insert data through form, But its not working. Please help:
Form Page Code:
<form class="form-horizontal" action="add_asc_submit.php" enctype="text/plain" method="post">
<div class="control-group">
<label for="asc_center_name" class="control-label">ASC Center Name</label>
<div class="controls with-tooltip">
<input type="text" name="asc_center_name" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_user_id" class="control-label">ASC User ID</label>
<div class="controls with-tooltip">
<input type="text" name="asc_user_id" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_user_password" class="control-label">ASC User Password</label>
<div class="controls with-tooltip">
<input type="password" name="asc_user_password" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_director_name" class="control-label">ASC Director Name</label>
<div class="controls with-tooltip">
<input type="text" name="asc_director_name" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_center_address" class="control-label">ASC Center Address</label>
<div class="controls with-tooltip">
<input type="text" name="asc_center_address" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_contact_number" class="control-label">ASC Contact Number</label>
<div class="controls with-tooltip">
<input type="text" name="asc_contact_number" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_center_city" class="control-label">ASC Center City</label>
<div class="controls with-tooltip">
<input type="text" name="asc_center_city" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<label for="asc_center_email" class="control-label">ASC Email Id</label>
<div class="controls with-tooltip">
<input type="text" name="asc_center_email" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
<br/>
<div class="control-group">
<div class="controls with-tooltip">
<input type="submit" name="submit" class="span6 input-tooltip" data-placement="bottom" required/>
</div>
</form>
add_asc_submit.php page details:
<?php
include('config.php');
if(isset($_POST['submit']))
{
if(! get_magic_quotes_gpc())
{
$asc_center_name=addslashes ($_POST['asc_center_name']);
$asc_user_id=addslashes($_POST['asc_user_id']);
$asc_user_password=addslashes($_POST['asc_user_password']);
$asc_director_name=addslashes($_POST['asc_director_name']);
$asc_center_address=addslashes($_POST['asc_center_address']);
$asc_contact_number=addslashes($_POST['asc_contact_number']);
$asc_center_city=addslashes($_POST['asc_center_city']);
$asc_center_email=addslashes($_POST['asc_center_email']);
}
else
{
$asc_center_name=$_POST['asc_center_name'];
$asc_user_id=$_POST['asc_user_id'];
$asc_user_password=$_POST['asc_user_password'];
$asc_director_name=$_POST['asc_director_name'];
$asc_center_address=$_POST['asc_center_address'];
$asc_contact_number=$_POST['asc_contact_number'];
$asc_center_city=$_POST['asc_center_city'];
$asc_center_email=$_POST['asc_center_email'];
/*}*/
$sql = mysqli_query($conn,"INSERT INTO add_asc(asc_center_name, asc_user_id, asc_user_password, asc_director_name, asc_center_address, asc_center_number, asc_center_city, asc_center_email) VALUES('$asc_center_name', '$asc_user_id', '$asc_user_password', '$asc_director_name', '$asc_center_address','$asc_center_number','$asc_center_city','$asc_center_email')");
if (mysqli_query($sql))
{
echo "ASC Added Succesfully.";
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
Config.php
<?php
$conn=mysqli_connect("localhost","root","", "rgcsm");
error_reporting(-1);
ini_set('display_errors', 'On');
?>
When I am running the following codes, Page redirecting me to add_asc_submit.php page, But not showing any error nor inserting data into Database. Please help in this...I did Googling also but not getting any success.
Many thanks in advance.
You run a query and store the result in $sql and then run a query again on the result?
Try:
<?php
include('config.php');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!get_magic_quotes_gpc()) {
$asc_center_name=addslashes ($_POST['asc_center_name']);
$asc_user_id=addslashes($_POST['asc_user_id']);
$asc_user_password=addslashes($_POST['asc_user_password']);
$asc_director_name=addslashes($_POST['asc_director_name']);
$asc_center_address=addslashes($_POST['asc_center_address']);
$asc_contact_number=addslashes($_POST['asc_contact_number']);
$asc_center_city=addslashes($_POST['asc_center_city']);
$asc_center_email=addslashes($_POST['asc_center_email']);
} else {
$asc_center_name=$_POST['asc_center_name'];
$asc_user_id=$_POST['asc_user_id'];
$asc_user_password=$_POST['asc_user_password'];
$asc_director_name=$_POST['asc_director_name'];
$asc_center_address=$_POST['asc_center_address'];
$asc_contact_number=$_POST['asc_contact_number'];
$asc_center_city=$_POST['asc_center_city'];
$asc_center_email=$_POST['asc_center_email'];
}
$sql = mysqli_query($conn, "INSERT INTO add_asc(asc_center_name, asc_user_id, asc_user_password, asc_director_name, asc_center_address, asc_center_number, asc_center_city, asc_center_email) VALUES('$asc_center_name', '$asc_user_id', '$asc_user_password', '$asc_director_name', '$asc_center_address','$asc_center_number','$asc_center_city','$asc_center_email')");
if ($sql) {
echo "ASC Added Succesfully.";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>