0

suppose i have a query like this :

$std_id =   $_POST['std_id'];
$name   =   $_POST['name'];
$family =   $_POST['family'];

$sql    =   "insert into student set
 std_id =   $std_id,
 name   =   '$name',
 family =   '$family'"; 
$query  =   mysql_query($sql,$conn); 

i read in a php security book that if user enter a value for family field like :

ahmad';drop database test#

can delete database test;

but we know that the mysql_query() function only allow to execute one query .
i want to know how can this input to be unsafe

str
  • 42,689
  • 17
  • 109
  • 127
Ahmad Badpey
  • 6,348
  • 16
  • 93
  • 159
  • possible duplicate of [Best way to stop SQL Injection in PHP](http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php) – Manse Nov 26 '11 at 11:06
  • @ManseUK The question seems to be *not* about how to prevent SQL-injection, but how come that the `mysql_query()` function executes two statements in this scenario. – Quasdunk Nov 26 '11 at 11:11
  • 1
    @Quasdunk SQL security is not always about running multiple statements - anything could be added to the end of that query that affects the insert – Manse Nov 26 '11 at 11:12
  • 1
    well gee I hope you add more "security" to your code before it goes live... – Adam Fowler Nov 26 '11 at 16:31

3 Answers3

3

Just worrying about multiple queries is not enough to protect SQL Security ... There are so many questions / answers on SO for you to read about this subject ..

Also good resources on php.net

Community
  • 1
  • 1
Manse
  • 37,765
  • 10
  • 83
  • 108
3

There are many delusions in your question.
Let's sort them out.

  1. mysql_query() doesn't support multiple queries execution.
    (so, it is useless to delete anything)
  2. dropping tables in the separate query is not the only way of the SQL injection.
    (so, it is useless to delete anything again)
  3. To protect your query you have to follow some well-known techniques, not some handmade inventions of doubtful efficiency.
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Using multiple queries separated by a semicolon is not the only way to exploit your queries, it is just a very simple example. It will work, when you are using mysqli_multi_query().

str
  • 42,689
  • 17
  • 109
  • 127