##
mysql> insert into comments set comments = 'foo';
Query OK, 1 row affected (0.00 sec)
mysql> select id from comments;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 38 |
| 39 |
| 59 | <--- RECORD CREATED SUCCESSFULLY
+----+
15 rows in set (0.00 sec)
mysql> delete from comments where id = 59;
Query OK, 1 row affected (0.01 sec)
####
mysql> desc comments\G
*************************** 1. row ***************************
Field: id
Type: int(10) unsigned
Null:
Key: PRI
Default: NULL
Extra: auto_increment
*************************** 2. row ***************************
Field: photologid
Type: int(10) unsigned
Null:
Key: MUL
Default: 0
Extra:
*************************** 3. row ***************************
Field: approved
Type: tinyint(3) unsigned
Null:
Key: MUL
Default: 0
Extra:
*************************** 4. row ***************************
Field: name
Type: varchar(255)
Null: YES
Key: MUL
Default: NULL
Extra:
*************************** 5. row ***************************
Field: comments
Type: mediumtext
Null: YES
Key:
Default: NULL
Extra:
*************************** 6. row ***************************
Field: comment_date
Type: datetime
Null: YES
Key: MUL
Default: NULL
Extra:
*************************** 7. row ***************************
Field: email
Type: varchar(255)
Null: YES
Key:
Default: NULL
Extra:
*************************** 8. row ***************************
Field: ipaddr
Type: varchar(255)
Null:
Key:
Default: 000.000.000.000
Extra:
####
#!/usr/bin/perl -w
use strict; use warnings;
# snip...
# connect to DB
$dbh = DBI->connect(
q[DBI:mysql:] .
qq[database=$DATABASE;] .
qq[host=$DBHOST;] .
qq[port=$DBPORT],
$DBUSER,
$DBPASS,
{ 'RaiseError' => 0, 'AutoCommit' => 1 }
)
or do {
# gently tell visitor we had an embarrassing uh-oh occur.
cgi_error(<<__ERR__);
An internal error has occurred on the webserver.
The server administrator has been notified of the error. It will be fixed
as soon as possible.
We appologize for the inconvenience.
__ERR__
# try to let administrator know there was a big problem
¬ify_admin('failed to connect to database! ' . $DBI::errstr);
};
my($photolog) = $dbh->selectrow_hashref(<<'__SQL__', undef, $plogid)
SELECT *,DATE_FORMAT(created,'%m/%d/%Y') AS fcreated FROM photologs WHERE id = ?
__SQL__
or do {
cgi_error(qq{Couldn't get photolog info for the requested photolog ID.});
notify_admin(<<__ERR__)
Problem getting photolog record id "$plogid"! $DBI::errstr
__ERR__
};
my($name) = $cgi->param('name');
my($email) = $cgi->param('email') || '[not specified]';
my($comments) = $cgi->param('comments');
cgi_error(q{Please go back and provide your comments. (Duh)})
unless defined $comments and length $comments;
cgi_error(q{Anonymous comments aren't accepted.
Please go back provide your name this time (or make up a good fake one.)})
unless defined $name and length $name;
$dbh->do(
<<'__SQL__',
INSERT INTO comments (
photologid,
name,
email,
comments,
ipaddr,
comment_date
) VALUES (?,?,?,?,?,NOW())
__SQL__
undef,
$plogid,
$name,
$email,
$comments,
( $ENV{'REMOTE_ADDR'} || '000.000.000.000' )
) or do {
cgi_error(qq{Couldn't add visitor comments. We're sorry!});
notify_admin(<<__ERR__)
Problem adding visitor comments to photolog id "$plogid"!
$DBI::errstr
__ERR__
};
my($msgid) = $dbh->{'mysql_insertid'};