How to include PHP variables in MySQL statements
P粉738248522
2023-08-23 21:12:18
<p>I'm trying to insert values into a table of contents. If I don't have PHP variables in VALUES it works fine. This doesn't work when I put the variable <code>$type</code> into <code>VALUES</code> . What did i do wrong? </p>
<pre class="brush:php;toolbar:false;">$type = 'testing';
mysql_query("INSERT INTO contents (type, reporter, description)
VALUES($type, 'john', 'whatever')");</pre>
<p><br /></p>
To avoid SQL injection, insert statements with be
The rules for adding PHP variables in any MySQL statement are simple and straightforward:
1. Use prepared statements
This rule covers 99% of queries, specifically yours. Any variable representing an SQL data literal (or, simply - an SQL string or number) must be added via a prepared statement. Without exception.
This method involves four basic steps
Here's how to do it using all popular PHP database drivers:
Use
mysqli
Add data textCurrent PHP versions allow you to prepare/bind/execute in a single call:
If you have an older version of PHP, preparation/binding/execution must be done explicitly:
The code is a bit complex, but a detailed explanation of all these operators can be found in my article, How to run an INSERT query using Mysqli , and a solution that significantly simplifies the process.
For SELECT queries, you can use the same method as above:
However, if you have an older version of PHP, you will need to perform a prepare/bind/execute routine and add a call to the
get_result()
method to get the familiar mysqli_result You can get data from it in the usual way:Use PDO to add data text
In PDO, we can combine the bind and execute parts, which is very convenient. PDO also supports named placeholders, which some people find very convenient.
2. Use whitelist filtering
Any other query parts, such as SQL keywords, table or field names, or operators - must be filtered by whitelisting .
Sometimes we have to add a variable that represents another part of the query, such as a keyword or identifier (database, table or field name). This situation is rare, but it's best to be prepared.
In this case, your variable must be checked against the list of values explicitly written in the script. My other articleAdding field names in ORDER BY clause based on user selectionexplains this:
This is an example:
After code like this, both the
$direction
and$orderby
variables can be safely put into the SQL query because they will either be equal to one of the allowed variants or will will throw an error.The last thing to mention about identifiers is that they must also be formatted according to the specific database syntax. For MySQL, it should be
backtick
characters around the identifier. Therefore, the final query string for our order example will be