0

I am trying to work on a login and registration page and I am making a new database and i can do it fine like this:

mysql_select_db("my_db", $con);

$sql = "CREATE TABLE Persons
(
  FirstName varchar(15),
  LastName varchar(15),
  Age int
)";

// Execute query
mysql_query($sql,$con);

and what I am trying to do is replace the "persons" with a string so that it makes a table but it is dynamic. I need a string because I am making a table for each user that registers and the new table's name is their username and password send through an md5sum encryption. I've tried this with no success it wont make a new table like the other one does :

mysql_select_db("my_db", $con);

$sql = "CREATE TABLE $data
(
  FirstName varchar(15),
  LastName varchar(15),
  Age int
)";

// Execute query
mysql_query($sql,$con);

Please help me. I've tried joining strings together with no luck so I need help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
evan.stoddard
  • 698
  • 1
  • 5
  • 22
  • 7
    Are you sure you don't just want to create a row in Persons for each person that registers? – Doug T. Oct 01 '11 at 01:59
  • 1
    It's better to use highlight your code and hit the `{ }` button at the top to format it. – Mike B Oct 01 '11 at 01:59
  • 6
    I firmly recommend against this design. All users should be contained in the same table as rows, not as separate tables. – Michael Berkowski Oct 01 '11 at 02:00
  • 1
    FWIW I did do a little research on parameterizing database (and table) names way back when in this question: http://stackoverflow.com/questions/6656636/is-there-any-safe-way-to-parameterize-database-names-in-mysql-queries – Doug T. Oct 01 '11 at 02:02
  • 1
    WHY do you want to create a table for each user? There's NO reason to do that. – Fabio Poloni Jul 10 '12 at 10:50
  • 1
    @evan.stoddard Wow, very young ;-) Good luck! – Fabio Poloni Aug 01 '12 at 10:55

2 Answers2

12

I'm trying to create a database table for each user who registers. What am I doing wrong?

You are creating a database table for each user who registers.

This is not the way you're supposed to use relational databases. You create a row for each new user, not a table. Of course you'll need a few extra columns in your Persons table, like the Password. I'd also recommend against storing the age because that's not constant. Store the Birthday instead.

INSERT INTO Persons (FirstName, LastName, Birthday, Password)
VALUES ('John', 'Smith', '1980-07-21', 'SOME HASH HERE');

I hope you don't take it wrong, but you need to slow down a bit and learn more about SQL before trying to write more code. Trust me, it'll be better in the long run.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • Thanks, your were right. I jumped right into this way to fast. I think I asked this question when I was 13 and after i learned a lot so thanks. – evan.stoddard Aug 01 '12 at 01:51
0

I'm looking back at some of the questions I've asked in the past in it's crazy to see how much I've learned in the past few years.

evan.stoddard
  • 698
  • 1
  • 5
  • 22