Auto creating table if not existing is causing error

1638 views php
9

I'm trying to create following table . query works in phpmyadmin but doesn't work in php script. pls point out mistake in my code. Thank You

    $sql = "CREATE TABLE IF NOT EXISTS `portal` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `message_sid` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `number` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1";
    if (mysqli_query($conn, $sql)) {
    echo "Table portal created successfully";
    } else {
        echo "Error creating table: " . mysqli_error($conn);
    }

I'm getting this error:

You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the 
right syntax to use near '`id` int(255) NOT NULL AUTO_INCREMENT,  
`message_sid` varchar(255) NOT NULL' at line 2

answered question

2 Answers

6

I have done this by checking if table exists in php, on false create a new table creating table would be best to export an already existing one with phpmyadmin and then just use it as query syntax

posted this
0

Take the single quotes off of your table and row names

also if you make the id a primary key,do not make it not null, this will cause a error.

posted this

Have an answer?

JD

Please login first before posting an answer.