Perl Source Code: dbedit.pl
#!/usr/bin/perl
use CGI qw(:standard);
use DBI;
use DBD::mysql;
use common;
use connect;
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
myUtils::headers1();
myUtils::headers2();
print myUtils::sidebar ("perl", "dbread", "pl");
if (param('id') == '') {
header ("Location: dbread.pl");
exit();
}
my $id = param('id');
# database handling: SELECT
$dbh = connect::my_connect ();
$sql = "SELECT * FROM parks WHERE id='$id'";
# echo "<p>The database query is: $sql</p>\n";
my $sth = $dbh->prepare ($sql);
$sth->execute();
my $row = $sth->fetchrow_hashref;
if (!$row) {
header ("Location: dbread.pl");
exit();
}
print <<middle;
<div id="content">
<h2>Perl: Edit the MySQL Database</h2>
middle
print_form ($row);
#-------------------------------------------------------------
if (param ('submit') && (
param('site') || param('state') || param('city') || param('lat') || param('lon')))
#-------------------------------------------------------------
{
# special handling for integers
my $lat = param('lat');
my $lon = param('lon');
my $state = param('state');
if ($lat == '') {
$lat = 0;
}
if ($lon == '') {
$lon = 0;
}
if (length ($state) > 4) {
$state = substr ($state, 0, 4);
}
print_feedback ($row, $state, $lat, $lon);
# database handling: UPDATE
$sql = sprintf (
"UPDATE parks SET site='%s', city='%s', state='$state', latitude=$lat, longitude=$lon WHERE ID=$id;",
param(site), param(city));
print $sql;
$sth = $dbh->prepare ($sql);
$sth->execute();
}
print <<foot;
<p><a href='dbread.pl'>Show the contents of this database table »</a></p>
<p><a href='source.pl?f=9'>Show Perl source code »</a>
</div>
foot
myUtils::footers();
#-------------------------------------------------------------
# functions
#-------------------------------------------------------------
#-------------------------------------------------------------
sub print_form
#-------------------------------------------------------------
{
print <<form;
<form name='parksform' action='dbedit.pl' method='POST'>
<fieldset class='db'>
<legend>Edit a park</legend>
<table>
form
my $row = $_[0];
print (" <tr><td>Park:</td><td><input type='text' name='site' value='$row->{'site'}' /></td></tr>\n");
print (" <tr><td>City:</td><td><input type='text' name='city' value='$row->{'city'}' /></td></tr>\n");
print (" <tr><td>State:</td><td><input type='text' name='state' value='$row->{'state'}' /> \n");
print (" <span>(4 characters max)</span></td></tr>\n");
print (" <tr><td>Latitude:</td><td><input type='text' name='lat' value='$row->{'latitude'}' /></td></tr>\n");
print (" <tr><td>Longitude:</td><td><input type='text' name='lon' value='$row->{'longitude'}' /></td></tr>\n");
print (" <input type='hidden' name='id' value='$row->{'ID'}' />\n");
print (" <tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>\n");
print (" </table>\n");
print (" </fieldset>\n");
print ("</form>\n");
}
#-------------------------------------------------------------
sub print_feedback
#-------------------------------------------------------------
{
my $row = $_[0];
my $state = $_[1];
my $lat = $_[2];
my $lon = $_[3];
print "<p>The following data was edited in the data file: </p>\n";
print "<table class='results'>\n";
print " <tr><th>Field</th><th>Old data</th><th>New data</th></tr>\n";
printf (" <tr><td>Park</td><td>$row->{'site'}</td><td>%s</td></tr>\n", param('site'));
printf (" <tr><td>City</td><td>$row->{'city'}</td><td>%s</td></tr>\n", param('city'));
printf (" <tr><td>State</td><td>$row->{'state'}</td><td>%s</td></tr>\n", $state);
printf (" <tr><td>Latitude</td><td>$row->{'latitude'}</td><td>%s</td></tr>\n", $lat);
printf (" <tr><td>Longitude</td><td>$row->{'longitude'}</td><td>%s</td></tr>\n", $lon);
print "</table>\n";
}