-1

A user adds an income or expense. If he classifies it as a recurring transaction, he picks from a frequency_dropdown containing (weekly, fortnightly, monthly, etc.). The user also inputs the start date and end date.

I want to make an increment to date as per the frequency selected. For example, if I have weekly then I want to add +1 week using the 'modify' function but this function is not making any modification to the date and is stuck in a continuous if loop.

My first query is successful and inserts the data in the database but the query inside the loop is not working since the date is not increased as per frequency selected.

  //db query is done here.
  $sde=$_POST['startdateexpense'];
  $ede=$_POST['enddateexpense'];
  $dt = new DateTime(str_replace("-", "/", $sde));
  $dtUntil = new DateTime(str_replace("-", "/", $ede ? $ede : date("m-d-Y")));
  
          
  $modifiers=[
    "Annually"=>"+1 year",
    "Daily"=>"+1 day",
    "Weekly"=>"+1 week",
    "Fort-nightly"=>"+2 weeks",
    "Monthly"=>"+1 month",
    "Quarterly"=>"+4 months",
    "Semi-Annually"=>"+6 months",
    "Annually"=>"+1 year"
  ];
  $modifier=$modifiers[$ExpenseFrequency];
  $dt->modify($modifier); 
  if($dt <= $dtUntil){
    $dt->modify($modifier);
    //db query is done here.
    }

INPUT Expected and actual output

Can anyone please help me solve this?

avi
  • 3
  • 5
  • Please understand what a [mcve] is. If your issue is isolated to the loop and the modification of the date, then remove ALL irrelevant information. We don't need to know where the data is coming from or where it is going. We should shouldn't see any superglobal variable declarations, nor irrelevant form submission data, nor any sql queries, nor any js scripting. Set up a small demonstration that expresses your challenge, your relevant code, and the desired output ...everything else is just noise that will slow down volunteers that may want to answer your question. – mickmackusa Sep 07 '20 at 03:36
  • Thanks for the response. Is the question readable and understandable now? – avi Sep 07 '20 at 04:40
  • Again, we don't need to know anything about the querying. You can just have a comment inside your loop that says `// I am executing a db query here`. This is because the extra variables and the insert queries have absolutely nothing to do with your question. Keep boiling it down to the essential parts. In doing this, you will hear less people tell you about how insecure/unstable your query is because you are not using a prepared statements (<-- that is your next issue to tackle). We DO need to see sample data for `$sde` and `$ede` and `$ExpenseFrequency`(not twice though). – mickmackusa Sep 07 '20 at 04:44
  • Now that you have whittled down the mass of code, it has become obvious that you are trying to insert a datetime object `$dt` into your `ExpenseDate` db table column -- this will not work, you are going to want to use `format()` on the object. If you want to echo out each modified datetime value in the loop, this will give volunteers a clear picture of the expected output. Show us what you would expected to see from your sample data. – mickmackusa Sep 07 '20 at 04:59
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 07 '20 at 10:32
  • Thanks again for helping out. I have further made changes to the question and added expected and actual output images. – avi Sep 09 '20 at 01:07
  • Thanks for the warning. I am learning to make code more secure and will do that as soon as I learn how to do it. Thanks for the response. – avi Sep 09 '20 at 01:09

1 Answers1

0
$modifiers=[
    "Annually"=>"+1 year",
    "Daily"=>"+1 day",
    "Weekly"=>"+1 week",
    "Fort-nightly"=>"+2 weeks",
    "Monthly"=>"+1 month",
    "Quarterly"=>"+4 months",
    "Semi-Annually"=>"+6 months",
    
  ];
  
  //$dt->modify($modifier); 
  if($sde <= $ede){
    $modifier= $modifiers[$ExpenseFrequency];

$modifier= $modifiers[$ExpenseFrequency];

This part needs to be inside loop with [ ] braces.

Format should be done to $sde to insert it into database.

    
    $sde = new DateTime($sde);
    $sde->modify($modifier);
    $sde= $sde->format('Y-m-d');
    echo $sde;
    $query=mysqli_query($con, "insert into expense(ExpenseDate,ExpenseItem,ExpenseCost,ExpenseCategoryName,Image) value('$sde','$item','$costitem','$ExpenseCategoryID','$image')");
avi
  • 3
  • 5