Blaine Hurtado
Blaine Hurtado

Reputation: 139

Cannot create SQL table with query variable as table name in PHP

I am passing a query variable in PHP. I would like to make it the table name, but I know there is probably a SQL syntax error. When I print the statement, the variable is passed meaning it works, but the database simply isn't created.

Here is my code for the creation of the database:

$DBName = "database_name";  
$sql = "CREATE TABLE '$DBName'.'$login' (  
    ClientID int NOT NULL AUTO_INCREMENT,   
    AgentClients varchar(15),  
    ClientTotal int  
    )";  
mysql_query($sql,$link);
where `$login = $_POST['login'];

Also, I'm not worried about security breaches at the moment, so don't worry about that.

Any insight would be greatly appreciated.

Upvotes: 0

Views: 1220

Answers (1)

JvdBerg
JvdBerg

Reputation: 21866

You must use backticks for your tablename, and not quotes:

$sql = "CREATE TABLE `$DBName`.`$login` (  
  ClientID int NOT NULL AUTO_INCREMENT,   
  AgentClients varchar(15),  
  ClientTotal int,
  PRIMARY KEY (`ClientID`) 
)";

Upvotes: 6

Related Questions