0

I need get values between two dates (including first and last ones). for example i've got form with two dates:

2013-12-01
2013-12-08

and i need get list/array etc. which will looks like:

2013-12-01
2013-12-02
2013-12-03
2013-12-04
2013-12-05
2013-12-06
2013-12-07
2013-12-08

and then insert them all into column in MySQL but in individual lines.So it should looks like this:

         1
     __________
 1 ; 2013-12-01
   ; ----------
 2 ; 2013-12-02
   ; ----------
 3 ; 2013-12-03
   ; ----------
 4 ; 2013-12-04
   ; ----------
 5 ; 2013-12-05
   ; ----------
 6 ; 2013-12-06
   ; ----------
 7 ; 2013-12-07
   ; ----------
 8 ; 2013-12-08

I've tried:

$id = $_POST['id'];

$from = $_POST['from'];
$to = $_POST['to'];


$a = new DateTime($from);
$b = new DateTime($to);
$b = $b->modify( '+1 day' );



$period = new DatePeriod($a, new DateInterval('P1D'), $b);

foreach($period as $dt) {
  $to = $dt->format('Y-m-d');
  $insertquery = mysql_query(" INSERT INTO `unavailable` (`$id`) VALUES (`$to`) ");
}

but it's not working...

  • 3
    **Warning:** you're using [a **deprecated** database API](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) yourself from. Oh, and your SQL syntax is incorrect, use quotes (`'`) to delimit strings, not backticks. And what isn't working? Does it eat your dog? – Marcel Korpel Dec 01 '13 at 13:43
  • 1
    @MarcelKorpel How do we appeal to Stack Overflow for a *What's not working, does it eat your dog* Markdown shortcut – Shai Dec 01 '13 at 13:56
  • @Shai I'm not too keen on Markdown shortcuts for content, please keep content and presentation separated. ;-) – Marcel Korpel Dec 01 '13 at 13:59
  • @MarcelKorpel I'm sorry, you're right, I'm just not thinking straight since the untimely death of my chihuahua, Rex. He never saw that garbage collector coming – Shai Dec 01 '13 at 14:01
  • I tried many solutions, sometims it doesnť srite anything, sometimes writes just one line. I'm not too expirienced, so i'll be very glad, whether someone could write me complete solution for this problem :-) – user3054465 Dec 01 '13 at 14:25
  • Yep, mistake was in my SQL syntax, i really used backticks insted of '' Thank you :-) – user3054465 Dec 01 '13 at 14:37

1 Answers1

-1

Your code works well, just make these change or use my code:

modify( '+1 day' ); $period = new DatePeriod($a, new DateInterval('P1D'), $b); foreach($period as $dt) { $to = $dt->format('Y-m-d'); $insertquery = mysqli_query($conn, "INSERT INTO schedule_table (id, schedule_date, amount) VALUES (NULL, '$to', '20') "); } ?>