0

Can someone tell me if this code is open to SQL Injection and why:

$x = $_REQUEST['id'];
$x = mysql_real_escape_string($x);
$del = "DELETE FROM Y WHERE id = ".$x;
mysql_query($del);
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Simone Cognom
  • 39
  • 1
  • 5
  • Basic rule: if you're **concatenating together** your SQL statement - you're **always** in danger of SQL injection. And **YES** this code of yours is in danger of SQL injection – marc_s Jun 08 '13 at 08:54
  • @RickHoving You are either trolling or being really unobservant. – vinczemarton Jun 08 '13 at 09:41
  • to solve the confusion about `mysql_real_escape_string` http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string so yes, it is still unsafe – scones Jun 08 '13 at 09:43
  • possible duplicate of [How to prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php) – Álvaro González Jun 10 '13 at 15:36

3 Answers3

2

It is; consider x being id, which leads to a query of

DELETE FROM Y WHERE id = id

deleting all the rows from the table.

Koterpillar
  • 7,883
  • 2
  • 25
  • 41
0

It is because you dont quote the x.

You can also use something simple like:

sprintf("DELETE FROM Y WHERE id = %u", $x);
Zaffy
  • 16,801
  • 8
  • 50
  • 77
-6

It shouldn't be as you use mysql_real_escape_string() to escape it

Vladimir Georgiev
  • 1,949
  • 23
  • 26