CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Clearing Up Single Quotation Mark

 



kuroneko1313
New User

Jul 31, 2012, 7:43 AM

Post #1 of 2 (835 views)
Clearing Up Single Quotation Mark Can't Post

Hello,
I have a Perl script that reads and parses from a text file and then do some SQL database updates and inserts.

My problem is there are some single quotation mark in the input text file that keeps messing up with the SQL Query.

I'm trying to use the Search and Replace function like $last_name =~ s/'/ /gi; but it never works out for me...

This is how my code looks like:

PARSING AND READING RESIDENT NAME FROM THE TEXT FILE:

$field_7 =~ s/^\s+//g; # Resident Name
$field_7 =~ s/\s+$//g;
$resident_name = $field_7;
$last_name = $resident_name;
$last_name =~ s/'/ /gi;
# SEARCH AND REPLACE A SINGLE QUOTATION WITH A SPACE
if ( index($resident_name," ") > 0 )
{
$first_name = substr($resident_name,0,index($resident_name," "));
$last_name = substr($resident_name,index($resident_name," ") + 1);
$last_name =~ s/'/ /gi;
# SEARCH AND REPLACE A SINGLE QUOTATION WITH A SPACE
}


THEN INSERT RESIDENT NAME INTO SQL DATABASE:

insert into IntegrationResident
values ( '$first_name'
,'$last_name') -- FAILED HERE BECAUSE OF A SINGLE QUOTATION MARK

I think this is because I'm not doing my Search and Replace correctly... Could anyone please help me on this one?

Thanks!



FishMonger
Veteran / Moderator

Jul 31, 2012, 7:50 AM

Post #2 of 2 (834 views)
Re: [kuroneko1313] Clearing Up Single Quotation Mark [In reply to] Can't Post

Don't put your input vars directly in the sql statement. Use placeholders and put those vars in the execute statement. The DBI module handles the escaping of any special characters as needed.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives