Drop Shadow

Modifying Rows

There are three basic ways of modifying data: inserting new rows, modifying existing rows, and deleting existing rows.

Insert new data

If you have a form that lets people add data to your database, you can use:

<? $insertFields = array('name', 'subject', 'age'); $insertID = $db->insert($insertFields); ?>

The insert() function returns the ID of the row that was just inserted. You can use this for related tables, or just to verify that the row was in fact inserted.

If you have data that is inserted according to your own criteria (for example, the username of the person who entered the data), you can add extra fields just as for the where() function.

<? $insertFields = array('name', 'subject', 'age'); $insertions = array('user'=>$authenticatedUser); $insertID = $db->insert($insertFields, $insertions); ?>

Update existing data

If you want to display a form that lets users update data, use displayForm() instead of display(). It otherwise works exactly the same way:

<? $showFields=array('name', 'age', 'subject'); $db->displayForm($showFields); ?>

In order to actually store updates sent via this form, you’ll also need to add the update() function, which works very much like the insert() function:

<? $updateFields=array('name', 'age', 'subject'); $db->update($updateFields); ?>

Inserting or replacing data at the same time

Sometimes you’ll have a form that displays data if the person has been here before, but asks for new data if they haven’t. You can perform an insert or update using the “replace()” function. It works just like insert(), but if a unique column already exists the row with that unique column will be updated.

You might use the ID for this, or another unique column such as MySanDiego account. You should be careful with this function: make sure you are aware of what unique columns you have in your table(s), and don’t let spurious updates happen. Generally, if you use replace(), you should have only one unique column in the list of columns you pass to replace().

Updating and displaying forms for multiple rows

There is a corresponding display(), displayForm(), insert(), replace(), and update() for handling lists of data: multiple rows of your table(s). Use displayList(), displayFormList(), insertList(), replaceList(), and updateList() if you’re working on more than one row at a time. The options are the same.

Deleting rows

Be very careful when deleting rows over the web! Once they’re gone, they’re gone.

To allow deletions to occur, use “delete()”. This method will both alert the system that it needs to provide a delete button on the form, and that it needs to delete any submitted items.

If you are only deleting from one table, that’s all you need to do:

<? $db->delete(); ?>

The delete() method will return the ID of the field that it deletes. You’ll need this if you have any related tables you need to delete data from. For example:

<? IF ($deletedItem = $view->delete()): $view->delete('item_destinations', $deletedItem); $view->delete('item_audiences', $deletedItem); ENDIF; ?>

The above examples don’t specify the name of the column in the foreign tables, so the system assumes that it is the local table and “ID”. If the current table is “item”, it will assume “itemID” in the foreign table. The above example will delete all rows in “item_destinations” whose “itemID” column is equal to the value of $deletedItem, and all rows in “item_audiences” whose “itemID” column is equal to the value of $deletedItem.

If you need to specify the column to match against, specify it as a third parameter.

<? $view->delete('item_destinations', $deletedItem, 'itemID'); ?>

When you specify the table to delete from, the method will return the number of rows deleted.