Avatar of pradapkumar
pradapkumar asked on

sql injection

What is an SQL Injection Attack / Vulnerability? In my official web site security audit, the following query found SQL Injection Attack / Vulnerability.. How to avoid this. please help urgent.
mysql_query("UPDATE users SET age='$age' WHERE id = '$id'");

thaks in advance.
MySQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon

From  Wikipedia,

SQL injection is a technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another.

mysql_query("UPDATE users SET age='$age' WHERE id = '$id'");
If the string $id is "foo' OR 'x'='x", the resulting query will be:

mysql_query("UPDATE users SET age=$age WHERE id = 'foo' OR 'x'='x'");
This will update age of every user, clearly not a good thing.

A safer way of doing this is:

if (get_magic_quotes_gpc()) {
  $age = stripslashes($age);
mysql_query("UPDATE users SET age='".mysql_real_quote_string($age)."' WHERE id = '".mysql_real_quote_string($id)."'");

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

thanks for immediate reply!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy