admanage - performance enhanced search technology

How to prevent MySQL injection in forms?

He has: 107 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,

Sponsor

He has: 470 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.

He has: 107 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: 1 posts

Joined: Apr 2008

Place a server side check along with the client side check. Barred the special charecter.

Sponsor

He has: 470 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);
?>