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>
Related
I have a Form with in php. I have it talking and inserting to the the MYsql database perfectly if i have all fields in the form filled in. Basically i have 3 text fields and 2 dates fields. i only want to chose either date field instead of both.
I have tried only inserting 1 date and it does not insert in to DB only when i have both dates chosen it works.
Below is the code which works when both date fields are chosen. i want to be able to only have 1 or the other or both if possible
<?php
require('db.php');
//Inserting Data Variables
if(isset($_POST['submit']))
{
echo $owner = $_POST['owner'];
echo $budget = $_POST['budget'];
echo $status = $_POST['status'];
echo $s_jan = $_POST['s_jan'];
echo $s_feb = $_POST['s_feb'];
//Query the database
$query = "insert into sbook(owner,budget,status,s_jan,s_feb)
values('$owner','$budget','$status','$s_jan','$s_feb')";
$insert_query = mysqli_query($con, $query);
}
?>
Here is the form
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<label>Owner</label>
<br />
<input class="input" type="text" name="owner">
<br />
<label>EST Budget:</label><br />
<input class="input" type="text" name="budget"/>
<br />
<label>Status:</label><br />
<input class="input" type="text" name="status"/>
<br />
<label>Jan</label><br />
<input class="input" type="date" name="s_jan"/>
<br />
<label>Feb</label><br />
<input class="input" type="date" name="s_feb"/>
<br />
<p>
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Above is the code which works when both date fields are chosen. i want to be able to only have 1 date or the other date or both if possible
<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">
This is the code for saving the information into mysql database from a FORM.
In the HTML section the form is being handled i.e. retrieving required data from user.
In the PHP section storing data has been handled.
But the problem is it doesn't store data.
I'm using XAMPP server.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="signup.php" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>
I don't understand what can be the problem.
Thanks all. I got the problem.
Actually the sequence of the column in my database was not matching with the query in php code.
I have solved this by changing the variable sequence in the query which is maintained in the database.
$query = mysql_query("INSERT INTO user (`name`, `mail`, `pass`, `address`, `phone`)VALUES('".$name."', '".$mail."', '".$pass."', '".$address."', '".$phone."')");
Here is the code and it will work for your..
I have passed connection link in your mysql_query. and used PHP_SELF for current page.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')",$connection);
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>
So I have a form that users fill out and it adds to a database. Everything works except for the date. The date shows up as 0000-00-00. Currently, I'm using a datepicker, but the date wasn't showing up even when the user had to type it in...so that shouldn't be the issue.
<form name="Add" id="Add" method="post" action="programadd.php">
<p>Program Name:
<input name="program" type="text" id="program" />
</p>
<p>Air Date
<input name="airdate" type="text" id="airdate" />
</p>
<p>Description
<input name="description" type="text" id="description" s />
</p>
<p>Production
<input name="production" type="text" id="production" />
</p>
<p>Promotions
<input name="promotion" type="text" id="promotion" />
</p>
<p>Community
<input name="community" type="text" id="community" />
</p>
<p>Web
<input name="web" type="text" id="web" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
And here's the code that adds it to the database:
<?php require_once('connect-db.php');
$program = $_POST['program'];
$airdate = $_POST['airdate'];
$description = $_POST['description'];
$production = $_POST['production'];
$promotion = $_POST['promotion'];
$community = $_POST['community'];
$web = $_POST['web'];
if (mysql_query ("INSERT INTO calendar(program, airdate, description, production, community, promotion, web) VALUES
('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />";
}
else {
die(mysql_error());
}
require_once("db_connx_close.php");
?>
One other question, how do I get the date to display as Month/Date/Year, instead of the reverse?
$date = date('Y-m-d', strtotime($_POST['airdate']));
I want to make a text field and button that will allow the user to fetch his details on the text fields instead of writting his details every time he wants to make a new reservation.
like in this picture:
http://oi41.tinypic.com/23ie70j.jpg
I tried to make this but with my code but gives me double forms one with the details and one without.
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$q = "SELECT * FROM tabe WHERE the_email = '$email'";
$run = mysql_query($q );
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}}
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
//limit the query to one entry :)
$q = "SELECT * FROM tabe WHERE the_email = '$email' LIMIT 1";
$run = mysql_query($q );
//check if email is registered
if(mysql_num_rows($run)>0)
{
//display filled up form
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}
}
//display blank form
else{
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<?php
}
}
?>
You're getting 2 forms because you're echoing one form if $_POST['submit'] is set and then another one regardless of anything. Print the second form only if $_POST['submit'] is not set. Since your code is so poorly written I will just give you an example:
if(isset($_POST['submit'])){
PRINT FETCHED FORM
}else{
PRINT EMPTY FORM
}
This, however, is not the "right" way to go. What people actually do is have variables null'd at start and then fill them up with data if there's a request and have a single form written in the file with input values as those variables.