For example this works fine:
$dropTable = $dbConnection->prepare('DROP TABLE IF EXISTS announcements');
$dropTable->execute();
$createTable = $dbConnection->prepare('CREATE TABLE announcements(
id MEDIUMINT NOT NULL AUTO_INCREMENT,
announcements TEXT NOT NULL,
PRIMARY KEY (id))');
$createTable->execute();
But this fails:
$dropTable = $dbConnection->prepare('DROP TABLE IF EXISTS :tableToDrop');
$dropTable->bindParam(':tableToDrop', $_GET['table']);
$dropTable->execute();
$createTable = $dbConnection->prepare('CREATE TABLE announcements(
id MEDIUMINT NOT NULL AUTO_INCREMENT,
announcements TEXT NOT NULL,
PRIMARY KEY (id))');
$createTable->execute();
With Error:
Fatal error: Uncaught exception 'PDOException' with message '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 '?' at line 1' in xxxx/createTables.php:9
Stack trace: #0 xxxxx/createTables.php(9):
PDO->prepare('DROP TABLE IF E...') #1 {main} thrown in xxxx/createTables.php on line 9
I'm sure it's something trivial, but I've been going at it for hours. Cheers.
Edit: Turns out you can't bindParam with a table name.Is there anyway to do a secure prepared statement with a dynamic table name?
$_GET['table']set with a value?