Jump to content

Recommended Posts

i keep getting an error when trying to create a table in a database using a username with a dash or underscore in it. the error is

"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 '-2010 ( `id` INT( 50 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` VARCHAR(' at line 1"  any help? is there a problem with the way i use $username?

 

here is the code i'm using

$username = htmlspecialchars($_POST['username']);
mysql_connect("$server", "$db_user", "$db_pass") or die(mysql_error());
mysql_select_db("singlese_messages") or die(mysql_error());
mysql_query("CREATE TABLE $username (
`id` INT( 50 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 75 ) NOT NULL ,
`subject` VARCHAR( 150 ) NOT NULL ,
`message` TEXT NOT NULL ,
`gift` VARCHAR( 50 ) NOT NULL,
`new` VARCHAR( 5 ) NOT NULL ,
`date_created` VARCHAR( 50 ) NOT NULL ,
`date_modified` VARCHAR( 50 ) NOT NULL 
)")  or die(mysql_error());  
mysql_close();

Link to comment
https://forums.phpfreaks.com/topic/203285-trying-to-create-username/
Share on other sites

If problem with $username then use next code

$username = htmlspecialchars($_POST['username']);
mysql_connect("$server", "$db_user", "$db_pass") or die(mysql_error());
mysql_select_db("singlese_messages") or die(mysql_error());
mysql_query("CREATE TABLE " . $username . " (
`id` INT( 50 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 75 ) NOT NULL ,
`subject` VARCHAR( 150 ) NOT NULL ,
`message` TEXT NOT NULL ,
`gift` VARCHAR( 50 ) NOT NULL,
`new` VARCHAR( 5 ) NOT NULL ,
`date_created` VARCHAR( 50 ) NOT NULL ,
`date_modified` VARCHAR( 50 ) NOT NULL )")  or die(mysql_error()); 
mysql_close();

 

btw why do you need to create table for every new user?

A proper database design could be:

 

CREATE TABLE message (
  id int(4) NOT NULL AUTO_INCREMENT,
  from_user_id int(4) NOT NULL,
  to_user_id int(4) NOT NULL,
  subject varchar(128),
  message text,
  
  FOREIGN KEY fk_message_from_user_id (from_user_id)
    REFERENCES user (id)
      ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY fk_message_to_user_id (to_user_id)
    REFERENCES user (id)
      ON UPDATE CASCADE ON DELETE CASCADE,
  PRIMARY KEY (id)
) ENGINE = InnoDB;

ok, i see where you're going, what does the bottom half do exactly?

 FOREIGN KEY fk_message_from_user_id (from_user_id)
    REFERENCES user (id)
      ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY fk_message_to_user_id (to_user_id)
    REFERENCES user (id)
      ON UPDATE CASCADE ON DELETE CASCADE,
  PRIMARY KEY (id)
) ENGINE = InnoDB;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.