FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How do you handle double quotation marks?





richard270384
Hi,

I have a SQL query which adds information provided by the user into a database but it obviously falls flat on its ass when the user string includes double quotation marks (") in the information.

How do you handle double quotation marks in your SQL queries? I have considered replacing them with single quotation marks but in same cases that will make the content look funny.

So what other options do I have? ANy sugggestions?

Cheers,
Richard
AftershockVibe
You should NEVER allow something which is going to be inserted into a MySQL statement to be used in this way. It's a security breach waiting to happen.

The simplest way to deal with this is to use the mysql_escape_string() around any text you wish to insert into a query. However, this only really deals with your quotes problem and not the rest of other possible security issues you might have in there.

For an exaplanation of why this is the case have a look at this wiki entry.
Article wrote:
Incorrectly filtered escape characters

This form of SQL injection occurs when user input is not filtered for escape characters and is then passed into a SQL statement. This results in the potential manipulation of the statements performed on the database by the end user of the application.

The following line of code illustrates this vulnerability:

Quote:
statement := "SELECT * FROM users WHERE name = '" + userName + "';"


If the "userName" variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the "userName" variable as

Quote:
a' or 't'='t


renders this SQL statement by the parent language:

Quote:
SELECT * FROM users WHERE name = 'a' or 't'='t';


If this code were to be used in an authentication procedure then this example could be used to force the selection of a valid username because the evaluation of 't'='t' is always true.

On MS SQL Server any valid SQL command may be injected via this method, including the execution of multiple statements. The following value of "userName" in the statement below would cause the deletion of the "users" table as well as the selection of all data from the "data" table:

Quote:
a';DROP TABLE users; SELECT * FROM data WHERE name LIKE '%


This input renders the final SQL statement as follows:

SELECT * FROM users WHERE name = 'a';DROP TABLE users; SELECT * FROM data WHERE name LIKE '%';


The whole article is probably worth a read. Hope this helps. Smile
richard270384
I knew there was a security risk I just didn't know how to handle it.

THanks a lot for the help.
Related topics
php variables
C++ TUTORIAL : INTRODUCTION TO C++
Using a pic as the main background of your page?
Who are your favriote Frihosters?
Which Statistics Software should I use?
Awstats
Visual Basic Tutorial - File I/O in VB: Text Files
Christianity: Why so many Churches? / Denominations?
State your Political Philosophy! (1000 FRIH$ to the best!)
Lucid Dreams
Invalid XHTML - Please help.
Communication in English
Create One-click Shutdown And Reboot Shortcuts
Plagiarism and the Role of the Internet [Pt. 2]
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.