I am using phpmyadmin
to create a table for an assignment and I am trying to figure out how to prevent SQL injection but I'm getting some weird error and I am not sure why. I am trying to use the quote
method to fix it but it's not working. Here is the code I have
try {
$conn = new PDO("mysql:dbname=*removed*;host=*removed*", "*removed*", "*removed*");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$firstName = $conn->quote($_POST["first_name"]);
$lastName = $conn->quote($_POST["last_name"]);
$house = $conn->quote($_POST["house"]);
$sql = "INSERT INTO my_table (last_name, first_name, house)
VALUES ('$lastName', '$firstName', '$house')";
$conn->exec($sql);
echo "<h3 id=\"success\">New entry successfully created.</h3>";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
I looked at the textbook I am using and it says to use the $db->quote($_POST["var_name"]);
to clean up the data, which is exactly what I do, but for some reason it's not working for me.
EDIT: The error message I am getting says this:
"INSERT INTO my_table (last_name, first_name, house) VALUES (''Allen'', ''Barry'', 'House1')
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Allen'', ''Barry'', 'House1')' at line 2
Any idea why?