Form submit is not entering data into database - php

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="duration" placeholder="Enter duration">
<input type="text" name="budget" placeholder="Enter Budget">
<input type="text" name="keyskills" placeholder="Enter Skills">
<input type="text" name="jobdescription" placeholder="Enter Job Description">
<input type="text" name="edate" placeholder="Click to enter expiry date">
<input type="text" name="cdexmin" placeholder="Enter Minimum Experience">
<input type="text" name="cdexmax" placeholder="Enter Maximum Experience">
<input type="submit">
</form>
<?php
if(isset($_POST['submit'])) {
try {
// Establish server connection and select database
$username = $_SESSION['username'];
$stmt = $db->prepare("SELECT * FROM employer INNER JOIN company ON employer.cid = company.cid WHERE employer.username='$username' ");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$cid=$row['cid'];
$eid = $row['eid'];
$duration = $_POST['duration'];
$budget = $_POST['budget'];
$keyskills = $_POST['keyskills'];
$jobdescription = $_POST['jobdescription'];
$edate = $_POST['edate'];
$cdexmin = $_POST['cdexmin'];
$cdexmax = $_POST['cdexmax'];
$stmt = $db->prepare("INSERT INTO job(cid,eid,duration,budget,keyskills,jdesc,edate,cdexmin,cdexmax) values('$cid','$eid','$duration','$budget','$keyskills','$jobdescription','$edate','$cdexmin','$cdexmax') ");
$stmt->execute();
echo "JOB POSTED SUCCESSFULLY";
} catch(PDOException $e) {
echo "Error occurs:". $e->getMessage();
}
}
?>
Here is my code that I just created for a sample form that is trying to insert the values into database. My problem is that the values are not entering the database.
Why is it not working? Is there an syntax error I can't find?
Page parsing is easily done and it's not showing any errors but values are not entering the database.

You are using $_POST['submit'] but there is no any input with this name.
Add name to your input type submit as follow
<input type="submit" name="submit">

Change
<input type="submit">
to
<input type="submit" name="submit">

You have change in code. Add NAME attribute for POST form.
<input type="submit" name="submit">

Related

$_GET not working for SQL query

I'm trying to use both $_GET and $_POST in an sql query. The following is my code:
<?php
$assignment = mysql_real_escape_string($_GET['name']);
echo "$assignment <br>";
if (isset($_POST['add'])) {
$user = $_POST['username'];
$text = $_POST['comment'];
$query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')";
mysql_query($query) or die('Error, comment failed to post');
}
?>
<h1>Add Comment</h1>
<form action="log_entry.php" method="post">
Name:<br/>
<input type="text" name="username" value="" />
<br /><br />
Comment:<br />
<textarea style="height:200px;" type="text" name="comment" value="" ></textarea>
<br /><br />
<input type="submit" name="add" value="Add Comment" />
</form>
However, the $assignment variable does not work in the query. It is echoed properly before the query is made but its value inside the table after the INSERT is completed is empty. What exactly is causing this?
Instead of trying to combine GET and POST, use a hidden input field:
<?php
$assignment = mysql_real_escape_string($_POST['name']); // Name is now in POST data, so swap this
echo "$assignment <br>";
if (isset($_POST['add'])) {
$user = $_POST['username'];
$text = $_POST['comment'];
$query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')";
mysql_query($query) or die('Error, comment failed to post');
}
?>
<h1>Add Comment</h1>
<form action="log_entry.php" method="post">
<!-- Add hidden input to carry the name -->
<input type="hidden" name="name" value="<?php echo $_GET['name']; ?>"/>
<!-- Rest of the form is the same -->
Name:<br/>
<input type="text" name="username" value="" />
<br /><br />
Comment:<br />
<textarea style="height:200px;" type="text" name="comment" value="" ></textarea>
<br /><br />
<input type="submit" name="add" value="Add Comment" />
</form>

Adding a record from html form to oracle database

I have a HTML form that I want to add a record to an Oracle database when somebody hits submit. The table is hooking up somewhat, the problem is that when somebody submits their information they come up as NULL values in the database table.
HTML:
<form name="myForm" action="/Add_File.php" onsubmit="return validateForm()" method="post"><!--Form-->
<fieldset>
<label class ="label1" for "name">First Name: </label>
<input type="text" name="fname"><br />
<label class ="label1" for "name">Surname: </label><input type="text" name="sname"><br/>
<label for="email">E-mail Address: </label><input type="text" name="email"><br />
<label for "address">Address: </label> <input type="text" name="address"><br />
<label class="label" for "Password">Select a Password: </label> <input type="password" name="pass"><br />
<label class="label" for "Password">Retype-Password:</label> <input type="password" name="pass2"><br />
</fieldset>
<fieldset><input class="button" type="submit" onclick="message()" value="Submit"/>
<input class="button" type="reset" value="reset form" onclick="myFunction()"/>
</fieldset>
</form>
PHP code:
$dbuser = "scott";
$dbpassword = "tiger";
$db = "orabis";
$conn = oci_connect($dbuser,$dbpassword,$db);
if (!$conn){
echo "Connection error";
exit;
}
$fname=$_POST['First_Name'];
$sname=$_POST['Surname'];
$email=$_POST['Email_Address'];
$address=$_POST['Address'];
$selpass=$_POST['Select_A_Password'];
$confirm=$_POST['Retype_Password'];
$sql = "INSERT INTO Become_A_Member_110385461(First_Name,Surname,Email_Address,Address,Select_A_Password,Retype_Password)
VALUES ('".$fname."','".$sname."', '".$email."', '".$address."','".$selpass."', '".$confirm."')";
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
echo "Error in preparing the statement";
exit;
}
oci_execute($stmt, OCI_DEFAULT);
print "Record Inserted";
oci_commit($conn);
oci_close($conn);
change like this
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$email=$_POST['email'];
$address=$_POST['address'];
$selpass=$_POST['pass'];
$confirm=$_POST['pass2'];

PHP Form will not post into my database

My PHP form will not post into the database. I understand php and mysql connections fairly well but I'm stumped on this one. When I hit submit on my form it doesn't echo the values that I(the user) put in. The date shows up as 1969-12-31, not the date the user submits. If anyone could help that would be great. My code is as follows
The form code is:
<form method="POST" action="add_event.php" id="create_event">
<label for="event_name">Event Name:</label>
<input type="text" id="event_name"><br />
<label for="date">Date:</label>
<input class="datepicker" type="date" id="date"><br />
<label for="zip_code">Zip Code:</label>
<input type="text" id="zip_code" maxlength="5"><br />
<label for="description">Description</label>
<textarea id="description" rows="5" columns="10"></textarea>
<br>
<input type="submit" name="submit">
</form>
The add_event.php insert code is:
<?php
require_once '../app_config.php';
require_once '../database_connection.php';
require_once '../authorize.php';
session_start();
// Authorize any user, as long as they're logged in
authorize_user();
//Get the user ID of the user to show
$user_id = $_SESSION['user_id'];
$select_query = "SELECT first_name, last_name FROM users WHERE user_id = " . $user_id;
// Run the query
$result = mysql_query($select_query);
if ($result) {
$row = mysql_fetch_array($result);
$first_name = $row['first_name'];
$last_name = $row['last_name'];
}
$name = $first_name . ' ' . $last_name;
$event_name = trim($_POST['event_name']);
$date = trim($_POST['date']);
$zip_code = trim($_POST['zip_code']);
$description = trim($_POST['description']);
// $date = "2012-08-22";
$newdate = date("Y-m-d", strtotime($date));
// $event_name = "test";
// $zip_code = "22153";
// $description = "test";
$insert_sql = sprintf("INSERT INTO events " .
"(name, user_profile_id, event_name, date, zip_code, description) " .
"VALUES ('%s', %d, '%s', '%s', '%s', '%s');",
mysql_real_escape_string($name),
mysql_real_escape_string($user_id),
mysql_real_escape_string($event_name),
mysql_real_escape_string($newdate),
mysql_real_escape_string($zip_code),
mysql_real_escape_string($description));
//insert the user into the database
mysql_query($insert_sql);
echo $insert_sql;
?>
Much thanks in advance.
The problem is that you are not naming your input fields. If you add the name property to the input in your html code the value will be stored in the $_POST array in php once the form is submitted. The correct html code should be:
<form method="POST" action="add_event.php" id="create_event">
<label for="event_name">Event Name:</label>
<input type="text" name="event_name"><br />
<label for="date">Date:</label>
<input class="datepicker" type="date" name="date"><br />
<label for="zip_code">Zip Code:</label>
<input type="text" name="zip_code" maxlength="5"><br />
<label for="description">Description</label>
<textarea name="description" rows="5" columns="10"></textarea>
<br>
<input type="submit" name="submit">
</form>
I am not sure if you needed the ids on the inputs for anything else, otherwise you should re-add those. For more information on html forms, visit: http://www.w3schools.com/html/html_forms.asp
You have not included any name attribute in your form. Here is how it should be:
<label for="event_name">Event Name:</label>
<input type="text" id="event_name" name="event_name"><br />
<label for="date">Date:</label>
<input class="datepicker" type="date" id="date" name="date"><br />
<label for="zip_code">Zip Code:</label>
<input type="text" id="zip_code" maxlength="5" name="zip_code"><br />
<label for="description">Description</label>
<textarea id="description" rows="5" columns="10" name="description"></textarea>
Note that only form inputs element that have the name attribute will be sent to the server. ID is only used on the client side.
Don't you need some name attributes in your form?
For example:
<input class="datepicker" type="date" id="date" name="date">
and the value within your POST['your_value'] must be the same as the value for the name attribute.
so:
$my_date = $_POST['date']
You need to give your input elements a name attribute... This is how php uses the $_POST['name attribute'] global to identify the field you are referring to.
so for example...
<form method="POST" action="add_event.php" id="create_event">
<label for="event_name">Event Name:</label>
<input type="text" name="event_name" id="event_name"><br />
<label for="date">Date:</label>
<input class="datepicker" name="date" type="date" id="date"><br />
<label for="zip_code">Zip Code:</label>
<input type="text" id="zip_code" name="zip_code" maxlength="5"><br />
<label for="description">Description</label>
<textarea id="description" name="description" rows="5" columns="10"></textarea>
<br>
<input type="submit" name="submit">
</form>

Clear html text fields

I have an HTML form for submitting and retrieves data from MySQL database with two buttons, "Save/Submit" and "New/Reset"
It fetch data correctly from MySQL database but when I click on New/Reset button for new contact entry it couldn't clear forms text fields. My HTML and PHP codes are as under:
<?php
//Database Connection file.
include'connect.php';
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'");
While($result = mysql_fetch_assoc($sql)){
$fname = $result['fname'];
$lname = $result['lname'];
$email = $result['email'];
$contact = $result['contact'];
}
if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&&
isset($_POST['contact'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname',
'$email', '$contact')")){
echo'Contact Save Successfully.';
}else{
echo'Contact not save.';
}
}
?>
<html>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>">
//Clean all fields of forms for new entry.
<input type="reset" value="New">
//Save or submit form data into mysql database
<input type="submit" value="Save">
</form>
</html>
You can do this easily by using jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#btnReset").click(function(){
$("#fname").val("");
$("#lname").val("");
$("#email").val("");
$("#contact").val("");
});
});
</script>
</head>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>" id="fname">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>" id="lname">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>" id="email">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>" id="contact">
//Clean all fields of forms for new entry.
<input type="reset" value="New" id="btnReset">
//Save or submit form data into mysql database
<input type="submit" value="Save" id="btnSave">
</form>
</html>

PHP won't post form to database

Trying to post a simple form to my database but can't get it to work. I have PHP and MySQL activated through XAMPP. The database "E-mail list" is set up with the table "Players".
PHP code:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = 'INSERT INTO Players (
name,
email,
phone,
other
)
VALUES ('.$name.', "'.$email.'", "'.$phone.'","'.$other.'")';
if ($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}}
?>
And the form:
<form id="myForm" method="post">>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
</form>
<p><strong id="error"></strong></p>
<br><br>
<input type="button" id="save" name="save" value="Submit Form" />
<p id="response"></p>
I did some changes in your codes both PHP and HTML Parts.,
For PHP :
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`) VALUES ('".$name."','".$email."','".$phone."','".$other."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
For HTML :
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
I think this may help you to resolve your problem.
Missing double quotes for name value in your SQL.
VALUES ("'.$name.'", "'.$email.'", "'.$phone.'","'.$other.'")';
Use Firefox/firebug to see the parameters and result, and add an echo($query); so you can see it in firebug.
'E-mail list' doesn't seem like convenient database name, though it should be okay.
Anyway, your goal should be to display all possible error that may occur.
So, you have to always check for the errors and report them in more usable form than just 'Cannot save data.'
Always check your connect
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if ($mysqli->connect_error) {
trigger_error($mysqli->connect_error);
}
same for the query
if (!$mysqli->query($query)) {
trigger_error($mysqli->error." ".$query);
}
If you see no error messages - check the logic of your code: if you ever run the code, if you run the code you wrote, if PHP works, typos etc.

Categories