简体   繁体   中英

PDO Create table not working

Just having trouble inserting a new table using PDO. The error I am getting is:

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 ''25500' ( id INT(11) NOT NULL AUTO_INCREMENT, date TIMESTAMP NOT NULL DEFAUL' at line 149440 does not exist!

Please note that $id is the table name.

function createTable($dbh, $id, $description)
{
try {
$stmt = "CREATE TABLE '$id' (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`prodtitle` VARCHAR(50) NOT NULL ,
`unitsize` VARCHAR(10) NOT NULL ,
`price` VARCHAR(10) NOT NULL ,
`wasprice` VARCHAR(10) NOT NULL ,
`specprice` VARCHAR(10) NOT NULL ,
PRIMARY KEY (`id`) ) COMMENT = '$description'";

if($dbh->exec($stmt) !== false) echo 'The sites table is created';
} 
catch (PDOException $e) { echo $e->getMessage();
}
}

It must be something simple but I just cant pick it.

TableName should not be wrap with single quote as it is not an string literal. Try to change single quote with backtick,

$stmt = "CREATE TABLE `$id` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`prodtitle` VARCHAR(50) NOT NULL ,
`unitsize` VARCHAR(10) NOT NULL ,
`price` VARCHAR(10) NOT NULL ,
`wasprice` VARCHAR(10) NOT NULL ,
`specprice` VARCHAR(10) NOT NULL ,
PRIMARY KEY (`id`) ) COMMENT = '$description'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM