LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2009, 09:21 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Problem with $dbh->quote in CGI script


Hi,
I'm trying to develop a website which will accept user written input in a form and will store it in a MySQL database, and will have no problems if the user written text has a ' in it. For this I was referred to this site:

http://www.developer.com/db/print.php/10920_2184681_6

and it looks pretty easy, what I have to do.
The problem is, when I use $dbh->quote, from my error logs I can see that it is substituting a ' with \\', ie. "O'Reilly" becomes "O\\'Reilly", instead of "O\'Reilly", and I can't tell why.

Can someone help?

Thanks.
 
Old 03-09-2009, 01:02 PM   #2
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Rep: Reputation: Disabled
I have always used prepare, this might work better for you.

Code:
my $last_name = "O'Reilly";
my $user_name = "resetreset";
my $sth = $dbh->prepare(qq{UPDATE users SET last_name = ? WHERE user_name = ?});
$sth->execute($last_name, $user_name);
 
Old 03-10-2009, 09:36 AM   #3
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
Yeah, I use prepare too, but I'm not sure what you're doing up there - what does $sth->execute($last_name, $user_name); do?
 
Old 03-10-2009, 09:58 AM   #4
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Rep: Reputation: Disabled
It properly quotes and substitutes those variables into the query (where the ?s are) and runs the query. Check out the documentation for a broader range of info -- http://search.cpan.org/~timb/DBI/DBI.pm
 
Old 03-13-2009, 10:16 AM   #5
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
OK, but I'm trying to use quote on data input by a form on an HTML page, so I can't use prepare. The page you suggested says this:
"For others it may return something like 'Don\'t' ", which I'm not getting.
So I'm still stuck at square one .

Thanks for your help anyway.
 
Old 03-13-2009, 12:53 PM   #6
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Rep: Reputation: Disabled
I have written database-driven web apps using Perl and MySQL that deal with data input from the user via form. The DBI prepare and execute methods have always worked for me; so the issue may be in your code. Post your code.
 
Old 03-14-2009, 01:49 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I can also say I've used dbh->quote() extensively and its always worked AFAICR.
I agree, pls show your code.
 
Old 03-16-2009, 02:59 AM   #8
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
OK, it's big but you asked for it (I'm posting what I think are the relevant parts) :

#! /usr/bin/perl

use DBI ;
use strict ;

use CGI ;
my ($cgi) = new CGI ;
my ($firstname) ;
my ($middlename) ;
my ($lastname) ;
my ($flat) ;
my ($street) ;
my ($city) ;
my ($state) ;
my ($country) ;
my ($postcode) ;
my ($phone) ;
my ($photo) ;
my ($type) ;
my ($rating) ;





$type = $cgi->param("type");





my ($dsn)="DBI:mysqlr:localhost";
my ($user_name) = "torrent" ;
my ($password) = "hello" ;
my ($dbh,$sth) ;
my (@ary) ;


$dbh = DBI->connect ($dsn, $user_name, $password, { RaiseError => 1 });


my ($firstname) = $cgi->param("firstname") ;
$firstname =~ s/(\w+)/\u\L$1/g;
$firstname = $dbh->quote( $firstname ) ;


, then later there's this:

$sth=$dbh->prepare (qq{ INSERT INTO people VALUES (NULL,'$firstname','$middlename','$lastname','$flat','$street','$city','$state','$country','$postcod e','$phone','$type','$rating')
$sth->execute ();
 
Old 03-16-2009, 08:28 AM   #9
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Rep: Reputation: Disabled
As I suspected, you are using prepare incorrectly. Take a look at my example and explanation.

This is how your code should be:
Code:
$dbh = DBI->connect ($dsn, $user_name, $password, { RaiseError => 1 });

my ($firstname) = $cgi->param("firstname") ;
$firstname =~ s/(\w+)/\u\L$1/g;

$sth=$dbh->prepare(qq{INSERT INTO people VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)});
$sth->execute (NULL,$firstname,$middlename,$lastname,$flat,$street,$city,$state,$country,$postcode,$phone,$type,$rating);

Last edited by int0x80; 03-16-2009 at 08:31 AM.
 
Old 03-17-2009, 01:45 AM   #10
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
It doesn't work, first of all it's refusing to take NULL as an argument, so I had to change that, after that it works (I had to change another INSERT in my code, further down the line). Do you know how I can put in a curdate() (in MySQL) into the $sth->execute line? ie. like this:
$sth->execute ($ary[0],$reviewername,$topic,$review, curdate()) ;
, but that's not the correct syntax, do you know what is?


Thanks a LOT for your help, btw. But it's still a niggling thought in my brain as to why $dbh->quote didn't work, I was hoping for chrism01's input on that - do you know why, chrism?

Last edited by resetreset; 03-17-2009 at 02:18 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
when to quote or not to quote variable names - mktemp problem glinux Linux - Newbie 3 02-20-2009 04:37 PM
CGI-Script Problem shipon_97 Linux - Newbie 1 05-09-2006 02:30 AM
Simple quote script? twirl Programming 2 10-15-2005 06:15 AM
CGI-script problem on Apache2 jkt2000 Linux - Networking 3 01-28-2005 04:38 AM
Apache cgi-script problem palanisaravanan Linux - General 6 02-07-2004 09:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration