Jump to content

Preventing SQL Injections + Being able to fetch an Array


Mko

Recommended Posts

Basically, I have the following code ($c2 is my connection variable):

$rid = $_GET['id'];
$q = mysql_query("SELECT * FROM reports WHERE id = $rid", $c2) or die(mysql_error());
$report = mysql_fetch_array($q);

 

$report is used later on to gather more information that is outputted to the user.

 

However, if in the URL, someone were to put id=1', they would have an error message spit out to them (something along the lines of: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'' at line 1), indicating a SQL Injection exploit.

 

How would I go about fixing this, and also preventing SQL Injection?

 

Thanks a bunch,

Mark

Link to comment
Share on other sites

die(mysql_error()) is an extremely useful tool for debugging, however i would not use it on production code. Once everything is fixed, i would handle it more efficiently like passing the error to user defined function, which logs/mails you and redirects user to more user friendly error message.

 

Showing half baked queries and error messages to the user is shabby and insecure especially to "Open to Public" websites.

 

PS: You still need to handle the errors :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.