Date not showing up when user submits form - php

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']));

Related

Can i allow an empty date field in a form?

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

Image not uploading to phpmyadmin

Im having some trouble uploading an image with php to phpmyadmin.
Here is my markup so far.
<form method="post">
<p>Event Date<span> - eg "23" for the 23rd</span><br /> <input type="text" name="eventDate" maxlength="2" required /> </p>
<p>Event Month<span> - eg "Mar" for March</span> <br /><input type="text" name="eventMonth" maxlength="3" required /> </p>
<p>Artist Image <br /><input type="file" name="artistImage" required /> </p>
<p>Artist Name <br /><input type="text" name="artistName" maxlength="30" required /> </p>
<p>Location <br /><input type="text" name="location" maxlength="30" required /> </p>
<p>Price <br /><input type="text" name="price" maxlength="10" required /> </p>
<p>Time <br /><input type="text" name="time" maxlength="10" required /> </p>
<input id="eventSubmit" type="submit" name="submitEvent" />
</form>
and here is my php code
`
if(isset($_POST['submitEvent'])){
$addEventDate = $_POST['eventDate'];
$addEventMonth = $_POST['eventMonth'];
$addEventName = $_POST['artistName'];
$addEventLocation = $_POST['location'];
$addEventPrice = $_POST['price'];
$addEventTime = $_POST['time'];
$addEventImg = $_FILES['artistImage']['name'];
$temp_name1 = $_FILES['artistImage']['tmp_name'];
move_uploaded_file($temp_name1, "img/$addEventImg");
$ticket= 1;
$insert_product = "INSERT INTO events (eventDate,eventMonth,artistImg,artistName,location,price,time,buyTicket) VALUES ('$addEventDate','$addEventMonth','$addEventImg','$addEventName','$addEventLocation','$addEventPrice','$addEventTime','$ticket')";
$run_product = mysqli_query($con,$insert_product);
echo mysqli_error($con);
if($run_product){
echo "<script>alert('Product has been inserted.');</script>";
echo "<script>window.open('admin.php','_self');</script>";
}
}
?>`
I have set the database up to take the image as a blob here is a picture of my database
and this is the code that is rendering the products onto the events page.
<?php
$get_events = "select * from events";
$run_events = mysqli_query($con,$get_events);
while($row_events = mysqli_fetch_array($run_events)){
$eventDate = $row_events['eventDate'];
$eventMonth = $row_events['eventMonth'];
$artistImg = $row_events['artistImg'];
$artistName = $row_events['artistName'];
$location = $row_events['location'];
$price = $row_events['price'];
$time = $row_events['time'];
$buyTicket = $row_events['buyTicket'];
echo "
<div class='eventBox'>
<div class='dateBanner'>
<p><span>$eventDate</span><br />$eventMonth</p>
</div>
<img src='img/$artistImg' alt='event image' />
<div class='eventDetails'>
<h2>$artistName</h2>
<p><span>Location</span> <br /> $location<br /><br /><span>Ticket Price</span> <br /> £$price<br /><br /><span>Time</span> <br />$time<br /><br /></p>
</div>
<div class='PurchaseTicket'>
<h3>Buy Ticket</h3>
</div>
</div>
";
}
?>
This works for everything apart from the image. The image just displays the alt tag not the actual image. It also doesn't save the image in the folder either.
If anyone could help I'd really appreciate it!!
Thanks

$_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>

Mandatory fields in the html form using PHP PDO

I have a html form where i want 3 fields to be mandatory. If the user doesn't fill any one of those fields, then the form shouldn't be submitted and it should tell the user to fill in the mandatory one's. I've used PDO and i dont know how to do it. If someone could help me. Down below i've given both my html and php files.
HTML:
<html>
<head>
<title>Data Insertion</title>
</head>
<body>
<p><span class="Error">* required field.</span></p>
<form method="post" action="su.php">
<h2>Please Fill In Details</h2>
<label for="name">Name </label>
<input type="text" Placeholder="Enter your name" name="name" id="name" />
<span class="Error">*</span>
<br />
<br />
<label for="age">Age </label>
<input type="text" name="age" id="age" placeholder="Enter your age" />
<br />
<br />
<label for="mailid">MailId </label>
<input type="text" name="mailid" id="mailid" placeholder="Enter your Mail Id" />
<span class="Error">*</span>
<br />
<br />
<label for="gender">Gender </label>
<br />
<label for="male">Male </label>
<input type="radio" name="gender" id="gender" value="Male" id="male" />
<label for="female">Female </label>
<input type="radio" name="gender" id="gender" value="Female" id="female" />
<br />
<br />
<label for="qualification">Qualification </label>
<select name="qualification" value="Qualification" id="qualification">
<option value="B.E">SSLC</option>
<option value="P.G">HSC</option>
<option value="SSLC">UG</option>
<option value="HSC">PG</option>
</select>
<br /><br />
<label for="hobbies">Hobbies </label>
<br />
<input type="checkbox" name="hobbies" id="hobbies" value="Cricket" />Cricket
<input type="checkbox" name="hobbies" id="hobbies" value="Music" />Music
<input type="checkbox" name="hobbies" id="hobbies" value="Swimming" />Swimming
<br /><br />
<label for="textarea">Address </label>
<br />
<textarea name="address" id="textarea" rows="15" cols="30"></textarea>
<span class="Error">*</span>
<br /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$servername;dbname=testing", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$mailid = $_POST['mailid'];
$gender = $_POST['qualification'];
$hobbies = $_POST['address'];
if($name !='' || $mailid !='' || $address !=''){
$sql = "Insert into user (Name, Age, MailId, Gender, Qualification, Hobbies, Address)
values ('".$_POST["name"]."', '".$_POST["age"]."', '".$_POST["mailid"]."', '".$_POST["gender"]."', '".$_POST["qualification"]."', '".$_POST["hobbies"]."', '".$_POST["address"]."')";
$conn->exec($sql);
echo "Thank you for registering";
} else {
echo "<p>Insertion failed <br/> Please enter the required fields !";
}}
}
catch(PODException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>
Try adding the html 5 attribute "required" on all the required input elements. For example
<input type="text" Placeholder="Enter your name" name="name" id="name" required />
You should also check the POST variables in the php code though, as this doesn't really prevent someone from abusing your service. Ex.
if(!isset($_POST['somevar'])) {
// Do insert
}
In html You should use javascript (e.g. jQuery) to control onsubmit event and validate if mandatory fields are filled with proper values, check this link: jQuery.submit()
In php You should check and validate each var before create and execute query. Simple article about it is here: Sanitize and Validate Data with PHP Filters
This would be good for the start I think ;)
Query string should contain placeholders and then statement should be prepared for execution, check this link:
PDOStatement::bindParam

php fetch your details from the database via email

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.

Categories