Search code examples
phpdatabasemysqlicreate-table

Creating table with the domain name fails


I am trying to create a table whenever the user submits his/her domain name. It is definitely going to be a .com or a .net or a .something The problem is that my code does not create a table when it contains a .anything it works fine for names and characters without a .something

$domain=$_POST['domain_name'];//it is dramatainment.com

$table = mysqli_query($connection, "CREATE TABLE $domain (
user_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_domain VARCHAR(50) NOT NULL,
user_email VARCHAR(50),
user_password VARCHAR(50),
user_date date
) ");
if(!$table)
{
 die('Could not create table: ' . mysqli_error($connection));
}

Does the creat table command have this limitation? Can this be solved?


Solution

  • You could try to replace the dot by an underscore.

    Here you can find which character is allowed in a table name.

    Additionally, it is possible to query using this syntax, which would conflict with your table name :

    SELECT * FROM dbname.table_name;
    

    UPDATE : it is possible using backticks to enclose table name :

    $sql = 'CREATE TABLE `$tableName` (
    user_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_name VARCHAR(100) NOT NULL,
    user_domain VARCHAR(50) NOT NULL,
    user_email VARCHAR(50),
    user_password VARCHAR(50),
    user_date date
    )';