0

Really looking for some guidance on this one.

I am currently creating an investment management portal for an organisation.

I have created a piece of code to generate the transactions that are due (code below), however when I have now tried to use this and change the echo to an sql insert query, it only creates a single record within the mysql database

$investment_term = 12;
$repayment_term = 1; //Monthly
$funds_received = date('2021-12-07');
$repayment_amount = 100;
$repayments = $investment_term / $repayment_term;  

for ($x = 1; $x <= $repayments; $x++) {  
$repeat = strtotime("+$repayment_term months",strtotime($funds_received));
$funds_received = date('Y-m-d',$repeat);

//Will replace below echo lines to sql insert query (query below);

echo "<p>Payment $x of $repayment_amount due on $funds_received</p>";
}
echo "<strong> Total of $repayments repayments</strong>">

//Sql Query to replace above echo's
INSERT INTO `transactional` (`investmentid`, `amount`, `duedate`, `postedby`, `status`) VALUES ('$investment_id', '$repayment_value', '$received_date', 'system', 'new');

What should happen is that there are 12 financial transactions created within the database with the each due date being +1 months

If anyone could assist or point me in the right direction that would be greatly appreciated.

  • (Possible) side note: Do not use string interpolation or concatenation to get values into SQL queries. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameterized queries. See ["How to include a PHP variable inside a MySQL statement"](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) and ["How can I prevent SQL injection in PHP?"](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – sticky bit Dec 07 '21 at 13:06

0 Answers0