On Saturday, August 2, 2003, at 08:48 PM, Bob Hicks wrote:
> I am new to Perl and HTML::Template/CGI-Application. I would love to
> see
> a real quick tutorial on using a database with these tools. Inserting
> data, populating tables and forms. Stuff like that.
>
>
really belongs to a DBI/DBD list + did you search the archives? A quick
search of the archives reveals this message I had posted sometime back.
http://bluedot.net/mail/archive/read.php?f=9&i=3325&t=3320
To summarize --
1. Get DBI (the interface) and the DBD (driver specific to your
database) of your choice working.
2. Query the database using fetchall_arrayref(). This returns an array
of hashes. Each element in the array is a row in your result set, and
each hash in that row is a database field with its corresponding value.
for example --
# a handler with your sql
my $sth = $dbh->prepare("SELECT user_id, fname, lname, email FROM
users");
# execute the handler
$sth->execute;
# a reference to the array of results
my $tbl_ary_ref = $sth->fetchall_arrayref({});
# cleanup
$sth->finish;
$dbh->disconnect;
# assign the reference to the results to a h::t var called results
$template->param(results => $tbl_ary_ref);
3. In your template you need to set up a <tmpl_loop> like so --
<table>
<tr><th>Full Name</th><th>Email</th></tr>
<!-- loop over results -->
<tmpl_loop results>
<!-- print out the values for each row -->
<form action="index.cgi" method="post">
<input type="hidden" name="user_id" value="<tmpl_var user_id>">
<tr>
<td><tmpl_var fname> <tmpl_var lname></td>
<td><tmpl_var email></td>
<td><input type="submit" value="edit"></td>
</tr>
</form>
</tmpl_loop>
</table>
The above html will create a table with one user's info per row
alongwith an edit button. Clicking the edit button will submit the form
with a hidden field containing that user's user_id. You can then use
that user_id to do further processing like editing user info, etc.
But, this should give you the general idea.
|