-5

Possible Duplicate:
Best way to stop SQL Injection in PHP

I have a question concerning the security of website. Actually I have used the queries as:

$name = $_POST['name'];
$sql = "INSERT INTO table (sno, name) VALUES('', '$name')";
mysql_query($sql);

Does this kind of coding assures the security from sql injection or others. If not what methods could we use to make our website secured?

Thanks in advance.

Community
  • 1
  • 1
Php Newbie
  • 11
  • 1
  • 7

3 Answers3

1

Use PDO with prepared statement and binding param

PDO TUTORIAL

Here's the start.

itachi
  • 6,323
  • 3
  • 30
  • 40
0

To protect yourself from sql-injections use PDO

In your case use mysql_real_escape_string($string) - but this is NOT ENOUGH SECURE. PDO )

s.webbandit
  • 16,332
  • 16
  • 58
  • 82
-5

Also you can use addslashes php function:

<?php
    $name = addslashes($_POST['name']);
?>
Valeriy Gorbatikov
  • 3,459
  • 1
  • 15
  • 9
  • @itachi as a matter of fact, there is nothing essentially wrong with them – Your Common Sense Apr 23 '12 at 07:05
  • @YourCommonSense: I didn't say addlashes has something wrong. Point is Use proper tools for appropriate fields. – itachi Apr 23 '12 at 07:07
  • addslashes is a quite proper tool. especially when compared to mysql_real_escape_string used as is – Your Common Sense Apr 23 '12 at 07:09
  • 1
    @YourCommonSense: One perfect reason not to use addlashes is this (and why it isn't a proper tool in sql making safe)... addslashes() can be tricked into creating valid multi-byte characters out of invalid ones. Whenever a multi-byte character ends in 0×5c (a backslash), an attacker can inject the beginning byte(s) of that character just prior to a single quote, and addslashes() will complete the character rather than escape the single quote. In essence, the backslash gets absorbed, and the single quote is successfully injected. This opens the door for SQL injection attacks. – itachi Apr 23 '12 at 07:13
  • @itachi that's what I am talking about. mysql_real_escape_string can be tricked as well. As well as PDO prepared statement – Your Common Sense Apr 23 '12 at 07:19
  • @YourCommonSense: totally agree with you. – itachi Apr 23 '12 at 07:22