• TOPIC 4 Active Server Page II
  • A database designed in ACCESS named "chi.mdb" contains 1 table named "account". This table has 2 fields, which are name and balance. The ODBC driver which will be created has the data source named "data1"

    An ASP was designed to provide the following three functions

      1. delete a record
      2. list all records
      3. update a record based on primary key
    IMPORTANT  INSTRUCTIONS

    File names in GREEN implies this is from table names
    RED à field names
    BLUEà variables passed between cfm pages

    How to delete a record
    File 1 (collect input data from users)
    File name is delete1.cfm

    <html>
    <head>
    <title>Search for a record</title>
    </head>
    <body>
    <form action="delete2.cfm" method="POST">
    Please enter name<input type="Text" name="n">
    <p>
    <input type="Submit" name="submit" value="SUBMIT">
    <input type="reset" value="RESET">
    </form>
    </body>
    </html>
     
    How to delete a record
    File 2 (delete a record)
    File name is delete2.cfm

    <html>
    <head>
    </head>
    <body>
    <cfquery datasource="data1" name="q2">
    delete account.* from account where name='#n#'
    </cfquery>
    <cfoutput>
    Account <B>#n#</B> Deleted.
    </cfoutput>

    <p>
    <p>
    <A HREF="f0.cfm"> GO BACK TO MAIN MENU</A>
    </body>
    </html>

    How to list all  records
    File3 (list all records)
    File name is list.cfm

    <html>
    <head>
    <title>Search for a record</title>
    </head>
    <body>
    <cfquery datasource="data1" name="q1">
    select * from account </cfquery>
    <cftable query ="q1" COLHEADERS HTMLTABLE BORDER>
    <CFCOL HEADER="Name" TEXT = "#name#">
    <CFCOL HEADER="BALANCE" TEXT="#balance#">
    </cftable>
    <p>
    <A HREF="menu.htm"> GO BACK TO MAIN MENU</A>
    </body>
    </html>
     
    How to update a  record
    File4 (allocate a record)
    File name is update1.cfm

    <html>
    <head>
     <title>Untitled</title>
    </head>
    <body>
    <form action="update2.cfm" method="POST">
    Please enter name<input type="Text" name="n">
    <p>
    <input type="Submit" name="submit" value="SUBMIT">
    <input type="reset" value="RESET">
    </form>
    </body>
    </html>
     

    How to update a  record
    File4 (display a record)
    File name is update2.cfm

    <CFQUERY DATASOURCE="data1" NAME="q1">
    select * from account where name = '#n#'
    </CFQUERY>
    <CFOUTPUT QUERY="q1">
    <HTML>
    <HEAD>
    <TITLE>Update Account - #name#</TITLE>
    </HEAD>
    <BODY>
    <H1> UPDATE ACCOUNT - #name#</H1>
    <FORM ACTION="update3.cfm" METHOD="POST">
    <INPUT TYPE="hidden" NAME="name" VALUE="#name#">
    <P>
    Balance:
    <INPUT TYPE="text" NAME="balance" SIZE="10" MAXLENGTH="10"
    VALUE="#Trim(balance)#">
    <p>
    <INPUT TYPE="submit" VALUE="UPDATE ACCOUNT">
    <INPUT TYPE="reset" VALUE="RESET">
    </FORM>
    </BODY>
    </HTML>
    </CFOUTPUT>

    How to update a  record
    File4 (update a record)
    File name is update3.cfm

    <CFUPDATE DATASOURCE="data1" TABLENAME="account">
    <CFOUTPUT>
    <HTML>
    <HEAD>
    <TITLE>Account #name# Updated</TITLE>
    </HEAD>
    <BODY>
    <H1> Account #name# Updated </H1>
    <A HREF="menu.cfm"> GO BACK TO MAIN MENU</A>
    </BODY>
    </HTML>
    </CFOUTPUT>

    Exercise

    1. Create a database using ACCESS.
    2. Use your last name as the database name. For example, smith.mdb
    3. Make one table in this database. Make sure you remember the table name.
    4. Create at least two fields; one of them should be the primary key.
    5. Use Notepad to create an list.cfm file (you can copy most codes above)
    6. Use Notepad to create an delete1.cfm file (you can copy most codes above)
    7. Design the page which will be used as the input page (remember the name for each input box)
    8. Create delete2.cfm
    9. Make datasource name = "your last name". For example datasource="smith"
    10. Change table name if different
    11. Modify SQL if necessary
    12. Code update1.cfm, update2.cfm and update3.cfm