-1

Here is the code snippet. I'm creating it for stock management in my shop:

$connect_stock = new PDO('mysql:host=localhost;dbname=daily_inventory', 'user', 'password');

$table_name = 'stock_'.date('dmY');
$query = "CREATE TABLE IF NOT EXISTS '$table_name' (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `item` varchar(250) DEFAULT NULL,
          `brand` varchar(250) DEFAULT NULL,
          `gender` varchar(250) DEFAULT NULL,
          `size` varchar(250) DEFAULT NULL,
          `sleeve` varchar(250) DEFAULT NULL,
          `fabric` varchar(250) DEFAULT NULL,
          `style` varchar(250) DEFAULT NULL,
          `wholesaler` varchar(250) DEFAULT NULL,
          `extra_features` varchar(250) DEFAULT NULL,
          `date_time` datetime DEFAULT CURRENT_TIMESTAMP,
          `cost_price` decimal(10,2) DEFAULT NULL,
          `quantity` int(11) DEFAULT NULL,
          `product_code` varchar(250) DEFAULT NULL COMMENT 'product_code will be set to Not Null',
          PRIMARY KEY (`id`)
         )";

try {
     $connect_stock->exec($query);
     echo "Created $table_name Table.\n";
   } catch(PDOException $e){
            echo $e->getMessage();
}

This is always printing:

Created {table_name} Table.

But no tables are getting added in the database. What can be the problem here?

Kshitij Kumar
  • 350
  • 4
  • 17

1 Answers1

1

Remove single quotes (' ') from table name

Use $table_name instead of '$table_name' and try. This will work definitely

phpforcoders
  • 326
  • 1
  • 9