#!/usr/bin/perl -w
#Define the behaviour of DBI->connect for the entire script
$ENV{DBI_DSN} ||= 'DBI:CSV(RaiseError=>1,AutoCommit=>1):'. join ';',
"csv_eol=\n",
'csv_sep_char=|',
'csv_quote_char=',
'csv_escape_char=';
use strict;
use DBI;
use CGI;
my $statement = q{
UPDATE list
SET title = ?
, description = ?
WHERE id = ?
};
my $cgi = CGI->new;
#Connect to the data source
my $dbh = DBI->connect;
$dbh->do(
$statement,
{},
$cgi->param('title'),
$cgi->param('description'),
$cgi->param('id'),
);
$dbh->disconnect;
print $cgi->header, 'Updated List';
__END__
Note: The above code assumes that your file name is
"list". DBD::CSV can only process files without any
extension. Also, if this is going into production you might want turn on Taint
checking by appending a T to the first line, as well as validating
each of the CGI paramters, possibly using HTML::FormValidator,
which is my personal favorite for this.
The great thing about using DBD::CSV is that if you ever switch
to using a relational database all the code won't need to change,
assuming your schema stays the same. This is why I explicitly define the environment variable $ENV{DBI_DSN} at
the top of the script, so that you can easily change it later.
Keep in mind this varible globally defines the data source
that all the DBI->connect method calls will
use, and can be globally set for all your CGI's in Apache's
httpd.conf.
As well, I do suggest you look into relational databases, like
MySQL or PostgreSQL.
They might be better suited for storing this type of information than
a flat file.
|