The Methods Behind the Banking Application Profile Script [con't]
The newacc.php code continued...
Below is a listing of all the methods used by the new accounts script:
The first of the methods is at the heart of the new account script. It is responsible for adding the new account to the database. The code starts by checking that a database connection exists:
Then it creates an SQL statement that is going to be used to insert the newly created customer information into the database:
The customer id is stored after being received from the mysql_insert_id() function. This function captures the auto generated ID of a new record that has just been created; we store this id in a variable called $newid:
The next step is to insert the newly created account details in to the accounts table, but to do that we need to have the customer ID. So we check to see if one is available by testing if the $newid variable is greater then zero:
If it is greater then zero then it means the newly inserted customer ID has been successfully captured and stored into the $newid variable. So we continue to insert the account details. First, we build the SQL statement that is required for this exercise:
Then we cater for any errors that might occur and return true if no errors occur:
The next method is used to generate a four-digit pin number:
First, we define the characters that we want our pin generator to use. Obviously all of the characters have to be numeric:
Then we initialize the retuen variable:
Now we get to the heart of the method. We run the for loop that will add a number to the above variable with each iteration until the length of the variable is four:
Then we return the pin number:
This method is not very different from the pin number generator above; the only difference is that the generated number is longer and that the method name is different:
The change pin script
This script enables a logged in user to change his or her pin number. The script is very easy to understand and is also very short. It provides a for element that will collect all of the information it needs to update the customers table where the pin number is stored. Below is a screen of the change pin page followed by the code listing of the script:
The code explained
The code starts by including the classes that are required by the script. These classes contain the methods that are required to execute certain parts of the page. We also instantiate the class that we want to use for this script:
Then we check to see if the form has been submitted:
We set some variables that we are going to use in the data validation process. The $err variable will work as the flag, that will determine if we are going to execute any database queries or not, and the $errmsg variable will be used to store the error message:
We then start with the validation process. This is easy to do since there is only one item that we need to validate and we know its length (four digits) as well as its type(numeric). All we need to do then is to make sure that the value is not empty:
Then we check to see if the value is numeric:
If no errors occurred, we call the change pin method to update the relevant database table:
The HTML
The HTML section of the page is also very short and easy to understand. It provides a form that collects the new pin number that the user wants. It starts by declaring the headers:
Then we build the navigation panel:
Finally, we build the form that hosts the field that actually takes the input from the customer:
That's all there is to the HTML part of this page. Next we look at some of the methods that have been used in this script.
Below are code listings of two methods, one is used in the script above and the other is used in a script that we will look at next:
The changepin() method is responsible for updating the customer table that contains the pin number. It starts with checking if a database connection exists:
Then it builds an SQL statement to do the update:
We allow for error, when running the query. The exception() function will contain the MySQL error that is generated if anything goes wrong with the query. The method returns false in the case of query failure and true on success:
The accountsadmin() method simply retrieves all the information about every customer in the database and then list the name and account type in an HTML table. At a later stage, we will add one more functionality to this method and that is how to delete accounts from the database:
The next installment will focus on the administration side of a banking application to speed up your application and keep it up to date.
Original: December 2, 2009