Update records
Username:  
Password:
  > Home
v User Guide
    > Introduction
    > Hello World
    > Flow Control
    > Function Calls
    > Authentication
    v Database access
       o Decode and encode
       o Searching
       o Display paging
       v Modify records
          o The matrix
          o Update records
          o Insert records
          o Delete records
    > Content Management
    > Remote Calling
    > Object-Oriented
    > Other Features
> Reference
> Portal Object
> Development Guide


Shortcuts
sys Class
debug Class
Intrinsic Conversions
>> User Guide >> Database access >> Modify records >> Update records <=  =>      <  1  >  
Update records

A database update operation (under the comment text "Update existing records" in the previous example) would look like:

database phbook action="update";

 

The operation loops through the "keys" variable, of which each element points to a record in the matrix (matrix%key).  An update query will be generated for each record.  The "set clause" of the update query is built with all the fields in the "fields" variable, with their corresponding values from the matrix record.  The field specified in the "keyfield" variable will be used in the "where clause".

The query would look like "update table set field1 = value1, field2 = value2, ... where keyfield = keyvalue".

If there is no "keyfield" specified, the "where clause" will be generated from the "search" variable as in the "select" query.

In the above example, the Birthday values will be encoded with the Conversion "strtotime" as specified in the phbook%encode:Birthday element.  This will automatically convert the date string (e.g. "10 Jan, 2003") into the timestamp format.

The query is sent to the database platform regardless whether the record has been changed or not.  If you want to selectively update some records, you will need to go through the matrix (looping through "(array){class%matrix:list}") and delete those record variables that you do not want to update.

(To find out which fields have been changed by the user is out of the scope of this document.  But here is a clue: you can program a hidden flag updated by a client JavaScript.  To avoid feeding the flag field into the "database" command, make sure you remove it after the selection loop.)

The operation will stop at the first error.  The error code and error message are contained in status%database and message%database respectively.  (For the integrity of the database object, you will have them in the database object as well.  In our example, they are phbook%status and phbook%message.)

 

Previous Page       Next Page

Accsoft Computer Technology Pty Ltd     ABN: 98 065 617 549
PO Box 892, Epping NSW 1710         Level 1, Epping Office Park, 242 Beecroft Rd, Epping NSW 2121, Australia
Tel: Sydney - (02)98691668     National - 1300-881668         Fax: (02)98691866
© Copyright 2003 Accsoft Computer Technology Pty Ltd