In the last article, we looked at the withdraw script that enables a customer to make a withdrawal from an account of their choice. In this article, we explore the mechanisms behind how to make a deposit into an account. We also look at the code that makes up the two methods that we briefly discussed in the previous article.
A method or two...
The two methods that we want to look at are the:
setbalance() Â This method updates the database table that hosts the account in question. More specifically, it updates the balance on that account. The method takes two parameters: the account number and the new balance. It has the following syntax:
setbalance(accountnumber,newbalance )
-
currbalance(accountnumber)
Below is the code for both the methods:
The currbalance() method is responsible for getting the current balance of the account, based on the account number that is fed into it. It starts by checking if a database connection is active or set:
Next, we build the SQL query that will be required to extract the data that we need:
Since it is a SELECT query we need to run it, the code does exactly that:
Now we test to see if any results have been returned. The results of the query are stored in the $res variable:
Note that an exception is created if an error occurs. If no errors were encountered during execution, we store the retrieved account balance in a variable called $row:
The current balance of the account is then stored in a variable called $currbal and returned to the function:
The setbalance() method is responsible for updating the balance of an account. Below is the code for the method:
It starts off by checking to see if a database connection is active or set:
Then we build an SQL query that will update or set the new balance:
We run the query and throw an exception if the query runs into any problems:
Unlike the currbalance() method, this method returns true or false. True if the query was successful and false if the query was unsuccessful:
The deposit.php script
The main purpose of this script is to provide the user with an opportunity to make a deposit to the preferred account. As you know by now, the homepage lists all the accounts of a particular user and provides two options; one of the options is to make a deposit. Below is screenshot of the page followed by the code that makes up this script:
The code
The code for the deposit.php script and the withdraw.php script is not so different. Both scripts make use of the setbalance and currbalance() methods. The code starts by including the accounts and customer classes after opening up a session:
A new object is then created:
Some variables are also declared. These two variables will be used to store error information:
Finally we check if the form has been submitted, then we validate the submitted data. The validation process involves checking if the data is empty or not and checking if the data is of the right type:
If no errors have been detected, then we continue to add the amount that the user wants to deposit to the balance of the account and store it in the $newbal variable:
Then we update the balance, using the setbalance() method that we discussed earlier:
The setbalance() method returns either true or false, depending on the query that it runs, so we test the $w variable to see which of the two it contains:
If all is well, the customer is transferred to the depositdone.php page that will display the outcome of the transaction:
The HTML portion of the script is identical to that of the withdraw script. Therefore, we will not spend much time going through its code. Basically, once the user selects the deposit option on the home page, this script is opened up and the account number is transferred to it. The form elements capture the account number and use it to run the currbalance() method. This method retrieves the current balance of the account and stores it in a hidden field of the form. The data is then sent to the PHP portion of the page and processed:
The account number, sent over from the homepage, is captured here and added to the currbalance() method:
Then it is stored in the hidden form element:
The amount that the user wants to deposit is stored in the form field:
The HTML form and table is then closed:
When the customer has made the deposit, he or she is transferred to a page called depositdone.php. That page has the following code:
Most of the HTML should be familiar to you by now, so I will only focus on the PHP section of the form that actually displays the out come of the transaction.:
The code above simply displays the new balance of the account. Below is a screen of the depositdone.php script:
In the next section, we will look at the customer profile script.