Possible duplicate:
Best way to prevent SQL injection in PHP
What is the best way to avoid strings when creating a query? mysql_real_escape_string () seems nice, but I donβt know exactly how to use it correctly.
Does this code work correctly?
<?php $newStr = mysql_real_escape_string($str); $query = "INSERT INTO table username VALUES ($str)"; mysql_query($query); ?>
EDIT:
Now I have this code:
$email = $_POST['email']; $displayName = $_POST['displayName']; $pass = $_POST['pass1']; $email = mysqli_real_escape_string($link, $email); $displayName = mysqli_real_escape_string($link, $displayName); $pass = mysqli_real_escape_string($link, $pass); $insert = "INSERT INTO profiles (email, displayName, password) VALUES ('$email', '$displayName', md5('$pass'))"; mysqli_query($link, $insert) or die(mysqli_error($link));
But I get this error: You have an error in the SQL syntax; check the manual for your version of MySQL server for the correct syntax to use next to "!" #! # ^! "#!" #! "# ^ '' '' '' on line 1
If the user enters: '**! "#! # ^!" #! "* #!" # ^ '' ''
string mysql escaping
Oskar persson
source share