I need some direction please. From my reading up on MYSQL I learned that to properly config my DB and keep it running optimally I need to make my DB as follows. Say for example I have
DB Members: which holds~ 1-Primary_ID, 2-email, 3-password, 4-address_ID, and 5-phone_ID.
Ok so now I know I need to make a new DB named DB: address and another DB: phone
DB Address: which holds~ 1-address_ID(UNIQUE), 2-street, 3-city, 4-state, 5-zip
finally another DB: phone
DB Phone: which holds~ 1-phone_ID(UNIQUE), 2-area code, 3-phone
Question #1: is this an efficient way to setup my database? && Is it correct?
Question #2: When using PHP to INSERT the record from an HTML form. How do i ensure that the address inputted in the form is assigned to DB address and the correct address_ID is recorded in DB Members? && same for phone_ID in DB Phone AND DB members?
Thanks for the help!
What should my mysql_query statement look like?
mysql_query("INSERT INTO Members (email, password, address, phone, timeStamp)
VALUES('$email', 'SHA($pass)', '$address', '$phone', now())");
How do I assign address to the address DB and phone to phone DB? Do i need to use separate insert statements? and if so- how do I get their proper ID’s into DB members?
Do not use
SHA()to hash your passwordsFrom the MySQL reference manual:
The mysql_* suite of functions should not continue to be used
Now, regarding your questions
It depends. Do you intend to only have one phone number per user, and one address per user? By specifying an address_ID field on the members table, you effectively limit the amount of addresses that member can be limited to to one. Alternatively, if you want to allow a user to have any number of addresses or phone numbers, the phone and address tables should have references to the member–not the other way around.
If you really do only want one address and phone number per member, you should include all of that information on the members table. This will prevent unnecessary joins, and you can always specify what you want to
SELECT.If you want multiple addresses and phone numbers, you still might want to denormalize and just use a single table for all of that data. You would just have to limit the user to some reasonable number of addresses or phone numbers.
If you use mysqli, use the
insert_idproperty. If you use PDO, use the lastInsertId() method.