1

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?

Engr Atiq
  • 163
  • 2
  • 16
  • Don't trust that you'll always get a nice domain name, use [prepared statements](http://php.net/manual/en/mysqli.prepare.php). – Script47 Aug 19 '16 at 15:59
  • Don't you want to actually be adding a row with this data into a pre-existing table called 'domains' or something? – Tom Wright Aug 19 '16 at 16:00
  • @tom I like your idea. But in my case I needed the tables created for domain names. Thank :) – Engr Atiq Aug 19 '16 at 17:15
  • Possible duplicate of [Special Characters in MySQL Table Name](http://stackoverflow.com/questions/10443050/special-characters-in-mysql-table-name) – Gerard Roche Aug 19 '16 at 18:58

2 Answers2

0

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
)';
vincenth
  • 1,732
  • 5
  • 19
  • 27
  • Thank you for the response. It worked. Can you explain what difference the backticks make that enables the query to process names with .com ? – Engr Atiq Aug 19 '16 at 17:11
0

Some special characters are invalid for identifiers such as table names.

See http://dev.mysql.com/doc/refman/5.7/en/identifiers.html

Gerard Roche
  • 6,162
  • 4
  • 43
  • 69