B
Bigus
Hi
I have a mySQL database with a table called "tickets" in it. There are 3
columns in the table: email | time | ticket.
I have 3 variables $email, $time and $ticket containing an email address,
Perl time and session ticket, respectively.
What I want to do is check if a row already exists in the database for the
email address contained in $email. If it does then perform an UPDATE, if it
doesn't INSERT a new row.
This is the basic code I have for this task:
======================
# check if row exists and update / insert as appropriate
@row = $db->selectrow_array("SELECT * FROM tickets WHERE email = '$email'");
if($row[0] =~ /\w+/){
$db->do("UPDATE tickets SET time = '$thetime', ticket = '$ticket'
WHERE email = '$email'");
}
else{
$db->do("INSERT INTO tickets (email,time,ticket) VALUES
('$email','$thetime','$ticket')");
}
# check for DB errors
if($db->errstr){
print $db->errstr;
}
========================
That works to a point, but it's not bullet-proof. ie: some email addresses
have, for example, a single quote in them like:
f.o'(e-mail address removed)
A quote causes the above to generate the following error:
You have an error in your SQL syntax near
'(e-mail address removed)','1065092739','618BA732FF2A739F6E')' at line 1.
Having looked at the DBI CPAN page I explored qq{..} and q{..}but can't see
to get them to work. That is, in the case of the INSERT row:
1) $db->do(qq{INSERT INTO tickets (email,time,ticket) VALUES
('$email','$thetime','$ticket')});
or
$db->do(qq{INSERT INTO tickets (email,time,ticket) VALUES
($email,$thetime,$ticket)});
comes up with the same ERROR as above.
q{..} doesn't apparently handle interpolated variables, so any ideas how I
can make this bullet proof, regardless of what characters $email might
contain in it?
Thanks
Bigus
I have a mySQL database with a table called "tickets" in it. There are 3
columns in the table: email | time | ticket.
I have 3 variables $email, $time and $ticket containing an email address,
Perl time and session ticket, respectively.
What I want to do is check if a row already exists in the database for the
email address contained in $email. If it does then perform an UPDATE, if it
doesn't INSERT a new row.
This is the basic code I have for this task:
======================
# check if row exists and update / insert as appropriate
@row = $db->selectrow_array("SELECT * FROM tickets WHERE email = '$email'");
if($row[0] =~ /\w+/){
$db->do("UPDATE tickets SET time = '$thetime', ticket = '$ticket'
WHERE email = '$email'");
}
else{
$db->do("INSERT INTO tickets (email,time,ticket) VALUES
('$email','$thetime','$ticket')");
}
# check for DB errors
if($db->errstr){
print $db->errstr;
}
========================
That works to a point, but it's not bullet-proof. ie: some email addresses
have, for example, a single quote in them like:
f.o'(e-mail address removed)
A quote causes the above to generate the following error:
You have an error in your SQL syntax near
'(e-mail address removed)','1065092739','618BA732FF2A739F6E')' at line 1.
Having looked at the DBI CPAN page I explored qq{..} and q{..}but can't see
to get them to work. That is, in the case of the INSERT row:
1) $db->do(qq{INSERT INTO tickets (email,time,ticket) VALUES
('$email','$thetime','$ticket')});
or
$db->do(qq{INSERT INTO tickets (email,time,ticket) VALUES
($email,$thetime,$ticket)});
comes up with the same ERROR as above.
q{..} doesn't apparently handle interpolated variables, so any ideas how I
can make this bullet proof, regardless of what characters $email might
contain in it?
Thanks
Bigus