Jump to content

Creating table based on user input


sincspecv

Recommended Posts

I need to set up a table in MySQL based on the users input in a form. i have the input assigned to a variable ($userdir) but when i run the script it just creates a table called '$userdir' instead of what the user typed in.

heres the code im using:
[code]
$userdir = $_POST['userdir'];
$connection = @mysql_connect('localhost', 'root', 'aboy15') or die(mysql_error());
$sql = 'CREATE TABLE $userdir ('
        . ' `date` DATE NOT NULL, '
        . ' `update` TEXT NOT NULL'
        . ' )'
        . ' ENGINE = myisam;';
$result = @mysql_query($sql, $connection) or die(mysql_error());
[/code]

what am i doing wrong?
Link to comment
Share on other sites

well, i have a forum about automotive performance. currently i am trying to create something so users can put up information about thier current project car. it's along the lines of cardomain (www.cardomain.com) but i want it to be more like a log. i want it set up so that each time the user updates their project it creates a new row, and in each row is the date and the update. i couldn't think of any better way to do this than to have a new table for each user. i thought about it for days and it was the best i could come up with. if you have a better idea i would be happy to hear it. this is my first big project with php. every other project, i've done was rather simple and usually had some sort of guideline.

thanks,
matt
Link to comment
Share on other sites

when i use double quotes i get this error:

[quote]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 '"$userdir" ( `date` DATE NOT NULL, `update` TEXT NOT NULL ) ENGINE = myisam' at line 1[/quote]
Link to comment
Share on other sites

Trust me, you do NOT want a new table for each user. You want 2 tables, one stores usernames and ids, the other stores project ids and that same user id. eg;

[code]
CREATE TABLE users (
  id INT PRIMARY KEY AUTO INCRIMENT,
  username
);

CREATE TABLE projects (
  id INT PRIMARY KEY AUTO INCRIMENT,
  users_id INT,
  date_added TIMESTAMP,
  data TEXT
);
[/code]

Then, anytime user 4 makes an update you would run a query like...

[code]
INSERT INTO projects (user_id,date_added,data) VALUES (4,NOW(),'here is my car');
[/code]

When he comes back in a few days to make an update, run another....


[code]
INSERT INTO projects (user_id,date_added,data) VALUES (4,NOW(),'ive painted the doors');
[/code]

Now, if you want to retrieve all of user 4's data, you'd simply run....

[code]
SELECT data FROM project WHERE user_id = 4;
[/code]

Now you have two tables that can hold an unlimitted number of users and updates.
Link to comment
Share on other sites

well i got this to work:
[code]$sql = "CREATE TABLE $userdir (date DATE NOT NULL, update TEXT NOT NULL) ENGINE = myisam";[/code]
but i think im gonna go with this:
[quote author=thorpe link=topic=121870.msg501991#msg501991 date=1168489919]
Trust me, you do NOT want a new table for each user. You want 2 tables, one stores usernames and ids, the other stores project ids and that same user id. eg;


[code]
CREATE TABLE users (
  id INT PRIMARY KEY AUTO INCRIMENT,
  username
);

CREATE TABLE projects (
  id INT PRIMARY KEY AUTO INCRIMENT,
  users_id INT,
  date_added TIMESTAMP,
  data TEXT
);
[/code]

Then, anytime user 4 makes an update you would run a query like...

[code]
INSERT INTO projects (user_id,date_added,data) VALUES (4,NOW(),'here is my car');
[/code]

When he comes back in a few days to make an update, run another....


[code]
INSERT INTO projects (user_id,date_added,data) VALUES (4,NOW(),'ive painted the doors');
[/code]

Now, if you want to retrieve all of user 4's data, you'd simply run....

[code]
SELECT data FROM project WHERE user_id = 4;
[/code]

Now you have two tables that can hold an unlimitted number of users and updates.
[/quote]

thanks a lot man. i just feel stupid.
Link to comment
Share on other sites

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.