How to prevent MySQL injection in forms?

They have: 105 posts

Joined: Mar 2006

Hi,

How can I prevent MySQL injection in text fields and forms? Can someone provide a code example?

Is it basically taking commas out of the user input?

Thanks,

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Running your user-inputted data through the mysql_real_escape_string() is all you need to do.

It escapes all characters that could be used to mess with a SQL query.

They have: 105 posts

Joined: Mar 2006

I tried using mysql_real_escape_string() by putting the variable that is being checked within the brackets:

mysql_real_escape_string($username);
mysql_real_escape_string($password);

Is that how you use mysql_real_escape_string()? Does that actually stop MySQL injections? I also have PHP checks that ensure the user enters only alphanumeric characters into the form.

Is this enough security for the form?

Thanks,

They have: 3 posts

Joined: Jul 2012

mysql_real_escape_string($myfield);
mysql_real_escape_string($myfied2);

Smiling

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Those functions return the reformatted string, so use it like this:

<?php
$username
= mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
?>

stokes1900's picture

They have: 55 posts

Joined: Oct 2010

when some one attack on your database to get information without your knowledge is called mysql injection. it is just a mysql query to get information from your database. for example from your login table attacker can see the passwords by injecting sql. it is done when we have not applied strong server side scripting.

for detailed information check link

http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

They have: 20 posts

Joined: Nov 2010

Microsoft UrlScan
Check SQL sintax

{links removed}

They have: 7 posts

Joined: Mar 2011

What mysql_real_escape_string does is take a string that is going to be used in a MySQL query and return the same string with all SQL Injection attempts safely escaped.

stokes1900's picture

They have: 55 posts

Joined: Oct 2010

Yes we can prevent from sql injection through real_escape_string..

They have: 59 posts

Joined: Aug 2011

A MySQL injection attack occurs only when the user has permission to write something that is used as part of a query?

What about the ways in which the user can only select the radio button / checkbox / drop-down lists .. They can not really do many things SQL?

They have: 2 posts

Joined: Aug 2011

You can use stored procedure for prevent SQL injections. I have tried stores procedures in MS SQL and it working fine when SQL injection queries applied.

They have: 10 posts

Joined: Mar 2012

<?php
md5(serialize(base64_encode(mysql_real_escape_string($username)));
md5(serialize(base64_encode(mysql_real_escape_string($password)));
?

It's a bit of an overkill (there is no point doing all that, then md5'ing it), but you get my point. For things such as usernames and passwords you only really need to compare them, not much else.

They have: 11 posts

Joined: Mar 2012

every one is talking about in this forum to avoid sql injection through "real escape string " . But can any one tell me that How can we make its best use. and how can we prevent attacker from writing sql query on our page.

They have: 1 posts

Joined: Mar 2012

Use

<?php
 
    $query
= mysql_query("SELECT id FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'"); 
   
?>

They have: 11 posts

Joined: Mar 2012

Thanks john for this example..

They have: 15 posts

Joined: Dec 2012

thanks for the help !!

They have: 8 posts

Joined: Dec 2012

Really nice thread quite informative thanks to all.

They have: 10 posts

Joined: Aug 2017

An SQL injection is a common type of attack that uses malicious SQL code.

A few things to do:

1. Input validation and sanitation – writing code to illegally identify user inputs. Note that it’s impossible, though, to cover all scenarios
2. Use a WAF – a Web Application Firewall – so even if your code isn’t perfect (no ones is), you are still protected. You can read a good description about SQL injections from one vendor.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.