Apple John Apple John - 1 year ago 50
PHP Question

Full Date Recognition in PHP

I created a form that has a simple dropdown datepicker on it. The problem is whenever i submit the form only the Year will be shown in my table. I checked my sql (date) field and only the Year is being updated. I need to show the Month and Day too.
Form:

<label>Date</label>
<td>
<select name="date">
<option>Day</option>
<?php
//Do Loop while
$d=0;
do{
$d++;
echo "<option>".$d."</option>";
}while($d<=30);
?>
</select>
<select name="date">
<option>Month</option>
<?php
//For Loop
$m=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
for($i=0;$i<count($m);$i++){
echo"<option>".$m[$i]."</option>";
}
?>

</select>
<select name="date">
<option>Year</option>
<?php
//While Loop
$y=2021;
while($y>=2013){
$y--;
echo "<option>".$y."</option>";
}
?>
</select>
</td>


My insert script:

{
if($_POST["operation"] == "Add")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
$statement = $connection->prepare("
INSERT INTO users1 (memorandum, titlee, image, date)
VALUES (:memorandum, :titlee, :image, :date)
");
$result = $statement->execute(
array(
':memorandum' => $_POST["memorandum"],
':titlee' => $_POST["titlee"],
':date' => $_POST["date"],
':image' => $image
)
);
if(!empty($result))
{
echo 'Data Inserted';
}
}

Answer Source

Looks to me like you have two issues that need to be addressed.

First, you have three inputs (<select>'s, specifically) that each use the same value for the name attribute. That's like trying to set a single variable three times in a row with different values - instead of having all the values, it will have just the last one that was set, which in your case is the year.

Try changing the name attributes for your selects to "day", "month", and "year", respectively, or anything of your choosing, and all 3 values will show up in the $_POST super global.

Second, your insert statement will need to use all three variables (day, month, and year) to construct an ISO date string that can be inserted into your database. Off the top of my head, most SQL implementations may not choke on dates that don't exist, but I would plan for your database to complain as the worst case scenario. Since your form allows me to submit February 31 as a date, you'll need to either reject that date, or adjust it, to March 3 for example.

I think there are a variety of ways to do this - mktime() and the DateTime class are the first two that come to mind for me.

Building a new date with either of those two options might look like the following:

//Use mktime()
$timestamp = mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
//timestamp is now an integer - a unix timestamp

//You can convert the timestamp to something human-readable if you wish
$date = date('Y-m-d', $timestamp);

//You can use the DateTime class for this as well
$dt = new \DateTime("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}");
$date = $dt->format('Y-m-d');

Once you have a date, you can pass that as the value to bind for your :date parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download