Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.