The Methods Behind the Banking Application Profile Script | WebReference

The Methods Behind the Banking Application Profile Script

By Leidago Noabeb


[next]

In this article, we will continue to look at some of the methods that were used by the customer profile script. Then we will move on and discuss the code behind the new accounts script.

The profile.php code continued...

Below is a listing of all the methods used by the profile script:

The first method is responsible for doing the donkey work of updating the customers table with the changes that the customer makes. It starts by checking if a connection to the database server exists:

Then the SQL statement is built that will actually do the updating:

we run the query using the mysql_query() function. An exception is shown if the query fails:

Otherwise the method returns true:

Now, because the method returns a result, we can check the outcome of the update like so:

This will at least give the user some idea as to what is going on. The next method that we are gong to look at is the getcustdetails() method. The name of the method screams out what its function is. The method takes only one parameter, which is the customer ID:

First we declare some global variables. These variables are later going to be used to retrieve individual pieces of information about the customer:

Then we test to see if there is a database connection to the server:

We then build the SQL statement:

We run the query using the mysql_query() function:

Now, we test the result of the query, which is stored in the $res variable. If it contains something, then we continue to fetch the result array:

And store its contents in the $row array:

Now we store the retrieved database information in the global variables that we declared earlier, and get the method to return true:

If the query does not succeed, then the method returns FALSE:

The getcustname() and getaddress methods return the name and address of the customer, respectively:

The newacc.php script

As with all things, there is a beginning; our customer accounts do not escape this reality. The script enables us to create new accounts. It provides an HTML form that will collect the information that it requires and provides a form processing capability. Below is a screen of what the script looks like and the code listing following thereafter:

Figure 1

The code explained

Since the purpose of the script is to collect information that enables you to create a new account, all of the fields on the form must be filled in. The form itself does a couple of things:

  • Collects new customer details, both personal and account
  • Auto generates an account number.

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 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:

Once all of the preliminaries are done, we check to see if the form has been submitted. We do this by testing if the submit form variable is set:

If so, we start the data validation process. All fields must be filled in, so we need to make checks that test for this. We also need to make checks that test for valid data type. The first part of the validation process will test the personal data that is sent by the form:

Now we test to see if the data is of the right type. We expect the data to be of type string:

Next, we test the account information that was sent by the form. Here we test the data type of the received form variables. Since the account number is auto generated, we do not really have to test for it, but just to be on the safe side:

If no errors were found, we continue with the execution of the code by generating a pin number:

A pin number is generated by the code, using the genpi() method:

Now we 'clean' the received variables using the mysql_real_escap_string() function that is specifically designed for this purpose:

Finally we post the information to the database using the newcusomer()method that is defined in the customer class:

If anything went wrong during the posting of the data to the database, we show an error message:

The HTML

As stated before, the HTML has a dynamic element to it. It is responsible for generating an account number, storing it and then sending it to the PHP portion of the script. First, we build the HTML headers:

Then in the body section of the page, we build a table and navigation panel:

Finally, we build the form that is going to do the job of collecting the information that we need from the user:

Also included in the form is the section of PHP code that displays any error that may have been detected during the form validation stages of the script:

The personal information section of the page only takes the name and address of the user. You can include more information about the user such as email address or phone number:

The accounts section takes the account type that the user wants to open and the amount that the user wants to deposit as an opening balance. The account number is auto generated by the geaccno() method as shown below:

The newly generated account number is then placed in the form element below, which will then transmit the number when the form is submitted:

Next, the account type is selected here. There are two account types:

  • Savings
  • Current Account

Below, the user is presented with that choice and is then given a dropdown box that will hold these values:

Finally, the user is expected to enter an opening balance:


[next]