0

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.

6
  • You're mixing "table" and "database" a lot. Has the database been created before this code? (I'm not even sure if that matters) But you might need to run CREATE DATABASE '$DBName' first? Commented Oct 29, 2012 at 19:26
  • echo $sql; whats that return? Commented Oct 29, 2012 at 19:26
  • Run the printed query in phpMyAdmin or any other mysql client and you will get all the errors. Commented Oct 29, 2012 at 19:26
  • table for each login, is not great structure, and i rally hope you are sanitising the post value. Commented Oct 29, 2012 at 19:27
  • Replace ' by ` and add PRIMARY KEY after AUTO_INCREMENT Commented Oct 29, 2012 at 19:30

1 Answer 1

6

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`) 
)";
Sign up to request clarification or add additional context in comments.

2 Comments

or omit them altogether and use no back ticks.
He's also using an AUTO_INCREMENT without defining a key. That will fail, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.