-2

I want to display each Month data. Consider Month starts today 2018-04-11 and ends on 2018-05-11, so the in between whatever products we will be added I want to display them.

Below is my query I am using:

$curYear = date('Y-m', time());
$MonthendDate = '11';
$MonthEnd = $curYear.'-'.$MonthendDate;

$today= date('Y-m-d');

$select_agent = mysqli_query($conn,"SELECT * 
                                    FROM disbursal_amt as D 
                                        INNER JOIN customer_details as c 
                                    WHERE (D.da_cid = c.cid) 
                                    AND (D.da_bt_id = '".$_POST["employee_id"]."') 
                                    AND (D.da_date BETWEEN '$MonthEnd' AND '$today') ");

But I am getting only current month data with above query.

The products will be added on daily bases and out put should generate of that month only.

Please help me out.

Akash M
  • 488
  • 3
  • 6
  • 18
  • 2
    Your question is both unclear and too broad. Can you post something that you tried where it may have failed you? That way it would have at least shown some effort on your part. Remember, we're always glad to help those who first help themselves. Speaking of "help"; you should go over the help area to familiarize yourself with what can and cannot be asked and what is expected of you https://stackoverflow.com/help - Doing this, will **greatly improve** your experience on Stack Overflow. – Funk Forty Niner Apr 11 '18 at 12:32
  • *"Please help me out."* - How? – Funk Forty Niner Apr 11 '18 at 12:33
  • If you are looking for sql Use BETWEEN – TarangP Apr 11 '18 at 12:34
  • Sorry for not giving much description @FunkFortyNiner. I will edit my question explain in clear. – Akash M Apr 11 '18 at 12:36
  • Yes tried but not getting exact output. above is my query. @TarangP – Akash M Apr 11 '18 at 12:45
  • **It would be really USEFUL to see how you calculate** `$MonthEnd` and `$start` as that is the most likely place for an issue in this code – RiggsFolly Apr 11 '18 at 12:52
  • You might like to TIDY your query a bit to `INNER JOIN customer_details as c ON D.da_cid = c.cid WHERE ......` – RiggsFolly Apr 11 '18 at 12:54
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Apr 11 '18 at 12:56
  • As I am beginner in PHP and MySQLi, will take a look on your suggestion regarding SQL Injection. Thank you @RiggsFolly for getting me back. – Akash M Apr 11 '18 at 13:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168740/discussion-between-akash-m-and-riggsfolly). – Akash M Apr 11 '18 at 13:08
  • Are you sure you have data going all the way into `2018-05-11` – RiggsFolly Apr 11 '18 at 13:14
  • Yes, I am getting data of only between 2018-04-11 to 2018-04-30 not till 2018-05-11. @RiggsFolly – Akash M Apr 11 '18 at 13:22
  • But you do HAVE data for 2018-05-01 to 2018-05-11 right?? – RiggsFolly Apr 11 '18 at 13:23

1 Answers1

0

Not sure about your $monthEnd , you can try

// One month from today
$monthEnd = date('Y-m-d', strtotime('+1 month'));
$today=date('Y-m-d');
SELECT * FROM disbursal_amt as D 
INNER JOIN customer_details as c 
WHERE (D.da_cid = c.cid) 
AND (D.da_bt_id = '".$_POST["employee_id"]."') 
AND (D.da_date BETWEEN '$today' AND '$monthEnd') 

WARNING: Your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Yogs
  • 78
  • 7
  • [Yours too](https://stackoverflow.com/questions/49775260/all-data-between-two-specific-dates-on-monthly-bases#comment86566405_49775260) – RiggsFolly Apr 11 '18 at 12:56
  • @RiggsFolly I understand your point and obviously is vulnerable to SQL injection and your suggestion is correct, but the answer is based on the way questioner has done. – Yogs Apr 11 '18 at 13:00
  • Fine, but you should be careful there are some SO users that will downvote an answer that uses SQL Injectable code for that very reason. In my experience either leave them unanswered, or make the effort to convert the code to use parameterised and bindable queries – RiggsFolly Apr 11 '18 at 13:03
  • 1
    Or at the very least add a warning to your answer, I took the liberty of adding an example to your answer, feel free to reuse that or something similiar in other answers – RiggsFolly Apr 11 '18 at 13:06
  • Also the two dates you have calculated by `$curYear = date('Y-m', time()); $MonthendDate = '11'; $MonthEnd = $curYear.'-'.$MonthendDate; $today= date('Y-m-d');` is always be same.So if you need to use your date calculation you have to use `$curYear = date('Y-m', strtotime('+1 month')); $MonthendDate = '11'; $MonthEnd = $curYear.'-'.$MonthendDate; $today= date('Y-m-d');` You can simply print your SQL statement to debug the actual statement created. – Yogs Apr 11 '18 at 14:24