Documentation
Scripting with Vigilante
Setting up forms to use Vigilante
The key to using Vigilante's database handling functions is to map the name of your HTML form fields with the fields in your database. You must use the exact syntax of your database field names on your forms.
In the tutorial database mymusic,
the database field names use initial capital letters, such as ArtistID,
SongTitle and LastName. In a MySQL installation of the content management
system Movable Type (version 2.6.4), database fields use lowercase and
underscores, such as mt_entry, mt_trackback, and mt_blog.
Example 3-3-1 shows an HTML form that collects various types of data, not all of it related to database maintenance.
Example 3-3-1
/*
These input fields collect data for the tutorial database, mymusic
*/
<input type="text" name="AlbumTitle">
<input type="hidden" name="AlbumID" value="<?=$albumID;?>">
/*
These input fields collect data for the Movable Type table, mt_entry
*/
<input type="text" name="entry_title">
<input type="hidden" name="entry_id" value="<?=$entry_id;?>">
//This hidden field is used for scripting purposes only.
<input type="hidden" name="do " value="update_database">
<input type="submit" value="Submit">
Example 3-3-2 shows a script that processes the form in Example 3-3-1.
Example 3-3-2
// Make sure $do is set to "update_database".
if ($do=="update_database")
{
//Convert the form input into an associative array.
$formData = AssignFormInputToArray();
//Retrieve the record from Albums to update ...
$result_albums = SelectRecord("Albums", "AlbumID=$AlbumID", "row");
//... and update it.
UpdateRecord($result_albums, 0, $formData, "Albums", "AlbumID=$AlbumID");
//Retrieve the record from mt_entry ...
$result_mt = SelectRecord("mt_entry", "entry_id=$entry_id", "row");
//... and update it.
UpdateRecord($result_mt, 0, $formData, "mt_entry", "entry_id=$entry_id);
}
In each of the UpdateRecord()
functions, Vigilante take only the portion of the form input relevant to the
table passed to the function. In the case of the update query to Albums, only
AlbumTitle and AlbumID are recognized. In the case of the update query to
mt_entry, only entry_title and entry_id are recognized.
In both queries, the hidden input field named "do" is ignored.
Vigilante's database handling allows you to build your HTML forms with as many or as few fields needed.
CAUTION: If your form includes names and values necessary only for scripting, make sure the syntax of those fields do not conflict with your database field names.