I;m confused about the rules for quotes. In a book I’m reading,
a query looks like this
$isbn = $_POST[‘isbn’];
$author=$_POST[‘author’];
$title = $_POST[‘title’];
$price=$_POST[‘price’];
$query = “insert into books values('”.$isbn.“', '”.$author.“', '”.$title.“', '”.$price.“')”;
So this inserts the POST values into the database. I’ve tried looking at the official php documentation but I haven’t been able to find exactly what I’m looking for. What are the rules for single quotes and double quotes when it comes to variables? And are the periods just concentation operators or in this context do they mean something else?
What book are you reading? If you are using mysqli (not mysql) in PHP you should really look at the prepared queries syntax as it will sanitize your input for you ensuring you don’t have unescaped single/double quotes that would otherwise break your query via concatenation.
Simply assigning the $_POST fields to variables is pointless. The $_POST fields are already variables.
What you should be doing when you move the values out of $_POST is to validate them to ensure that they do not contain characters that are invalid for that particular field to contain. That way you can then distinguish between the $_POST variables that might contain invalid data and the other variables which you know are valid.
Then you do as cpradio suggested and use prepare and bind statements to insert the data into the database in a way that keeps the data separate from the SQL and so allows valid quotes in the data to be processed correctly.