Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

David Palmer
David Palmer
4,571 Points

Creating financial transactions within php / mysql

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.

I would also like to note that the above code isn't ready to avoid SQL injections and other vulnerabilities but it's only going to be used by 1 person to help track transactions.

Glen Rose
Glen Rose
7,108 Points

Hi

Just a quick note.
You should filter your inputs before adding them to your Database.