Jump to content

[SOLVED] insert into problem with dates


j33baS

Recommended Posts

I have the following code ...

 

<?php

if($_POST['submit']) {

$validdate = checkdate($_POST['month'], $_POST['day'], $_POST['year']);

if($validdate == TRUE) {
$concatdate = $_POST['year']
. "-" . sprintf("%02d", $_POST['day'])
. "-" . sprintf("%02d", $_POST['month']);

$datesql = "INSERT INTO test(date) VALUES($concatdate);";

mysql_query($datesql);
header("Location: " . $config_basedir . "/success.php");
}

else {
echo "date error"; 
}
}

else {
require("header2.php");
}
?>

<h2>Add New date</h2>

<!-- ******** Date selector ******** -->
<form method="post">
<table>
<tr>
<td>Day</td>
<td>Month</td>
<td>Year</td>
</tr>
<tr>
<td>
<select name="day">
<?php for($i=1;$i<=31;$i++){ echo "<option>" . $i . "</option>"; } ?>
</select>
</td>
<td>
<select name="month">
<?php for($i=1;$i<=12;$i++){ echo "<option>" . $i . "</option>"; } ?>
</select>
</td>
<td>
<select name="year">
<?php for($i=2008;$i<=2020;$i++){ echo "<option>" . $i . "</option>"; } ?>
</select>
</td>
<td><input type="submit" name="submit" value="Go"></td>
</tr>
</table>
</form>
<!-- ******** END Date selector ******** -->

<!-- ******** VIEW DATE ******** -->
<?php
//NOW()
$titlequery = "SELECT * FROM test ORDER BY date DESC";

$titleresult = mysql_query($titlequery);
while($row = mysql_fetch_array($titleresult)){
echo "<p>";
echo date("D jS F Y",strtotime($row['date']));
echo "</p>";
      }
    ?>

 

but theres a problem when the selected date is inserted into the database (test, which only has fields date(type date :P and id which is auto incrementing and the PK) and all thats being inserted is 0000-00-00.

 

$concatdate should be inserting it as mysql's favorite YYYY-MM-DD format with the code for leading zero's and the hyphens in there as well!

 

Was wondering if anyone could see any glaring errors ?! thanks

Link to comment
https://forums.phpfreaks.com/topic/88543-solved-insert-into-problem-with-dates/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.