Can't Parametize Table Names in Prepared Statements
Despite your initial attempts to separate variables for protection against SQL injection, you've encountered errors. The issue stems from trying to include the table name as a parameter.
In a prepared statement, parameterization is limited to values within the SQL statement. The table name, which determines the statement's validity and column names, cannot be dynamically substituted.
Even in interfaces like PDO, which simulate prepared statements, substituting the table name as a string within quotations leads to invalid SQL syntax.
To prevent injection vulnerability, it's best to maintain a white-listed set of acceptable table names and validate your $mytable against it. Code like "SELECT * FROM {$mytable}" remains a viable option, provided that you ensure $mytable's validity.
The above is the detailed content of Why Can't I Use Table Names as Parameters in Prepared Statements?. For more information, please follow other related articles on the PHP Chinese website!