I'm having trouble getting this to work. I've searched this site and found many other posts regarding this, but none seem to be working.
Here is my simple code:
if (isset($_POST['submit']))
{
$startDate = strtotime($_POST['from']);
$endDate = strtotime($_POST['to']);
for($i = $startDate; $i <= $endDate; $i = strtotime('+1 day', $i))
{
$date = date('Y-m-d',$i);
//echo $date . "<br>";
$sql = ("INSERT INTO `calendar` (`Cal_Date`) VALUES (`$date`)");
}
if(!$result = $db->query($sql))
{
die('There was an error running the query [' . $db->error . ']');
}
}
The form is just as simple and the dates are entered in "YYYY-MM-DD" format. What I'm trying to do is populate my database table with a range of dates. The only thing that happens is one row gets inserted and it is "0000-00-00" and I suspect this is because I've got that column set to Date, Not Null. When I echo the results, everything works perfectly, it's just getting it into the db doesn't seem to work. I've tried many other combinations of the INSERT
line including:
$sql = $db->query("INSERT INTO `calendar` (`Cal_Date`) VALUES (`$date`)");
($db is from db_connect.php )
$sql = ("INSERT INTO `calendar` (`Cal_Date`) VALUES (`{$date}`)");
$sql = ("INSERT INTO calendar ('Cal_Date') VALUES ($date)");
$sql = ("INSERT INTO `calendar` (`Cal_Date`) VALUES (`$date`)");
...and I think even a couple of others. I do know that my db_connect.php is connecting to the database as I've got:
$sql = ("SELECT * FROM calendar");
further down the page and it's working fine. I've been going at this for far too long and I'm convinced I'm just missing something obvious. I would appreciate your feedback.
mySQL 5.5.24 PHP 5.3.13 Apache 2.2.22