Search code examples
phpmysqlcreate-table

what is the mysql error in following?


This is to create a new table with name as users id

$tablename=$_SESSION['lgn-id'];

mysql_query("CREATE TABLE $tablename(
qtno INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(qtno),
 qt VARCHAR())")
 or die(mysql_error());  

echo " so I created one!";

It's showing the following error table name being [email protected]

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com( qtno INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(qtno), qt V' at line 1


Solution

  • Your create statement should be

    CREATE TABLE `$tablename`(
    qtno INT(10) NOT NULL AUTO_INCREMENT, 
    qt VARCHAR(100),
    PRIMARY KEY(qtno))
    

    And Creating separate table for each user is bad idea, lets say you have 1000+ users, do you want to create 1000+ tables? create table for post like UserId, Post and store userid from user table and post

    You can create Posts table like

    CREATE TABLE UserPosts
    (
       id INT(10) NOT NULL AUTO_INCREMENT,
       UserId Varchar(100),
       UserPost varchar(1024),
       InserDate DateTime,
       PRIMARY KEY(id)
    )
    

    Then you can do

    select UserId, UserPost FROM UserPosts Where UserId = '[email protected]'