Listing 1: add-client.html
<HTML>
<!-- add-client.html, an Embperl document -->
<Head><Title>Add a client</Title></Head>
<Body>
<!-- Perform some initialization -->
[! use Apache::DBI;
$dbh = DBI->connect("DBI:mysql:test:localhost");
@colnames = qw(id name address1 address2 city
state country zip contact_name
contact_phone1 contact_phone2
contact_fax initial_contact_date
dollars_per_hour);
$colnames = join ', ', @colnames;
!]
[$ if scalar keys %fdat > 0 $]
<!-- Make sure we avoid setting the ID -->
[- @insert_colnames = grep !/^id$/, @colnames; -]
[- $insert_colnames = join ', ',
@insert_colnames; -]
<!-- Create the query -->
[- $values = join '", "', map {$fdat{$_ .
"-0"}} @insert_colnames -]
[+ $sql = "INSERT INTO Clients
($insert_colnames)
VALUES (\"$values\")"; +]
<!-- Send the SQL -->
[- $sth = $dbh->prepare($sql); -]
[- $sth->execute; -]
<!-- If error, print a message -->
<P><B>[+ $sth->errstr +]</B></P>
[$ endif $]
<!-- Here is the manual HTML part of the file, for entering new clients -->
<H1>Add/Modify Client Information</H1>
<Form method="POST" action="/embperl/add-client.html">
<Table border="2">
<!-- Create table for entering a new client -->
[$ foreach $column @colnames $]
[$ if $column ne "id" $]
<TR> <TD>
[+ $column +]
</TD><TD>
<input type="text" name="[+ $column +]-0"
size="40" maxlength="40" >
</TD> </TR>
[$ endif $]
[$ endforeach $]
</Table>
<P><input type="submit" value="Add/update values">
<input type="reset" value="Revert values"></P>
</Form> </Body> </HTML>
Copyright © 1994 - 2019 Linux Journal. All rights reserved.