I am trying to create an SQL query to insert user info into a database. The $fname
and $lname
variables contain correct values ("John" and "Doe") but the query fails. Here is my code:
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$sql = "INSERT INTO users (fname, lname) VALUES ($fname, $lname)";
mysqli_query($conn, $sql);
after checking the error message I've found that this query fails with error saying
Unknown column 'John' in 'field list'
How can I properly include variables into SQL query to make it run without an error?
- This question is not about SQL syntax which I am quite familiar with, but about the rules of creating a query dynamically using variables in a PHP script.
- I don't want a quick patch that only eliminates the immediate error, but a state-of-art solution which is error free and secure as well.