| | |
How to properly use tables in db?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
I don't really know how to explain this easily. I guess I will layout the project I am trying to do, and the problem I am having with it, and maybe someone could explain it better to me.
I am trying to build an online food journal for a fitness/nutrition company. I have a form page made out that will allow their clients to input their eating habits for the day.
http://befit4riding.com/food_journal.html
I was thinking that I would have a db for each client, and each day would be a new table. I need to be able to store each day of data and keep it accecible for a later time. I also need to email the data to the company each time new data is submitted. Turns out, the server I am working on only allows 1 db. So how do I organize the tables so that they are all under the same client, but I can have multiple clients? Is there some kind of 'sub-table' (db/table/sub-table)? If I am not explaining myself adequately, please ask some specifics and I will be happy to try again.
notes on the page. I didn't have it automatically input the date, because sometimes people don't update everyday, so they need to be able to come in and put in multiple days at once, but have the correct date for each one. Eventually I might put in a calendar function, but I would have to learn that one and trying to just get this going first.
Thanks.
I am trying to build an online food journal for a fitness/nutrition company. I have a form page made out that will allow their clients to input their eating habits for the day.
http://befit4riding.com/food_journal.html
I was thinking that I would have a db for each client, and each day would be a new table. I need to be able to store each day of data and keep it accecible for a later time. I also need to email the data to the company each time new data is submitted. Turns out, the server I am working on only allows 1 db. So how do I organize the tables so that they are all under the same client, but I can have multiple clients? Is there some kind of 'sub-table' (db/table/sub-table)? If I am not explaining myself adequately, please ask some specifics and I will be happy to try again.
notes on the page. I didn't have it automatically input the date, because sometimes people don't update everyday, so they need to be able to come in and put in multiple days at once, but have the correct date for each one. Eventually I might put in a calendar function, but I would have to learn that one and trying to just get this going first.
Thanks.
oops. I just read the announcement that daniweb doesn't want us linking to our sites because of spam reasons. I in no way was trying to promote the site, I just linked it to give a reference for what I was trying to do. If this is a violation, could one of the mods please remove the link from my post? I can't seem to find an edit post feature here.
•
•
•
•
I don't really know how to explain this easily. I guess I will layout the project I am trying to do, and the problem I am having with it, and maybe someone could explain it better to me.
I am trying to build an online food journal for a fitness/nutrition company. I have a form page made out that will allow their clients to input their eating habits for the day.
http://befit4riding.com/food_journal.html
I was thinking that I would have a db for each client, and each day would be a new table. I need to be able to store each day of data and keep it accecible for a later time. I also need to email the data to the company each time new data is submitted. Turns out, the server I am working on only allows 1 db. So how do I organize the tables so that they are all under the same client, but I can have multiple clients? Is there some kind of 'sub-table' (db/table/sub-table)? If I am not explaining myself adequately, please ask some specifics and I will be happy to try again.
notes on the page. I didn't have it automatically input the date, because sometimes people don't update everyday, so they need to be able to come in and put in multiple days at once, but have the correct date for each one. Eventually I might put in a calendar function, but I would have to learn that one and trying to just get this going first.
Thanks.
I think what you want to do is create relationships between your tables. (no db's don't have sub tables). Since your working with a relational database, you can create relationships between tables that simulates "sub-tables".
You don't need a db for every client, or table for every client. This is very inefficient.
What you want is a single table for your clients. Each client should have a unique index.
Then you want another table storing information about clients. Each info has a unique index, and another index with a value that corresponds to the index in the client table. This lets you know that this data is for that client.
If you need different data for different days stored, just create a column where you save a timestamp in your client data table.
It would be easier to understand if you have a good book on relational databases to guide you.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
> I in no way was trying to promote the site, I just linked it to give a reference for what I was trying to do. If this is a violation, could one of the mods please remove the link from my post?
I cannot speak for the other mods, but I am more lax about this rule in the web development forums. What really pisses me off is when people post in the Internet marketing forums questions like, "Please give me suggestions on how I can promote my website www.mysite.com". It's also very hard to have candid discussions related to Internet marketing with the constant fear that everyone knows who you are and your competitors are there to see what you're doing and how you're doing it ... a general rule of not mentioning your site allows much more unbiased conversations because people are free to share their experiences about what worked and what didn't with regards to making money, advertising, etc,.
I cannot speak for the other mods, but I am more lax about this rule in the web development forums. What really pisses me off is when people post in the Internet marketing forums questions like, "Please give me suggestions on how I can promote my website www.mysite.com". It's also very hard to have candid discussions related to Internet marketing with the constant fear that everyone knows who you are and your competitors are there to see what you're doing and how you're doing it ... a general rule of not mentioning your site allows much more unbiased conversations because people are free to share their experiences about what worked and what didn't with regards to making money, advertising, etc,.
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Ok, after doing some reading, I think I got an idea of how to structure the table, client_logs. I can have a row for the client, with a specific date, and then all the fields from the page for that date. Then when I want to pull up a specific day of log entry, I can query the db with
That is what I got from my research on this, but it looks like digital-ether, has a slightly different method, which sounds like it might save time in the long run. Any suggestions/critiques/ways to do it better on this approach?
SELECT * FROM client_logs WHERE client='value', date='value'That is what I got from my research on this, but it looks like digital-ether, has a slightly different method, which sounds like it might save time in the long run. Any suggestions/critiques/ways to do it better on this approach?
•
•
•
•
Ok, after doing some reading, I think I got an idea of how to structure the table, client_logs. I can have a row for the client, with a specific date, and then all the fields from the page for that date. Then when I want to pull up a specific day of log entry, I can query the db with
SELECT * FROM client_logs WHERE client='value', date='value'
That is what I got from my research on this, but it looks like digital-ether, has a slightly different method, which sounds like it might save time in the long run. Any suggestions/critiques/ways to do it better on this approach?
Do you have a separate table for clients?
So you're query would be like:
SELECT * FROM client_logs WHERE client={clientid}, date='{timestamp}' LIMIT 1where the {clientid} is the index of the client's row in the clients table and {timestamp} is any date/time format.
Note the LIMIT 1. It's good practice to let the DB know that you only want a single entry. The reason is that the DB stops iterating through its index as soon as it reaches the first valid row. Otherwise, it goes through all rows. If you have something like 100 000 rows, this makes a huge difference.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
•
•
Thats pretty much the same as what I mentioned.
Do you have a separate table for clients?
So you're query would be like:
SELECT * FROM client_logs WHERE client={clientid}, date='{timestamp}' LIMIT 1
where the {clientid} is the index of the client's row in the clients table and {timestamp} is any date/time format.
Note the LIMIT 1. It's good practice to let the DB know that you only want a single entry. The reason is that the DB stops iterating through its index as soon as it reaches the first valid row. Otherwise, it goes through all rows. If you have something like 100 000 rows, this makes a huge difference.
Thanks for the note on the LIMIT 1.
•
•
Join Date: Aug 2007
Posts: 189
Reputation:
Solved Threads: 14
I recommend every-time as possible the guideline use mentioned by digital-ether. I will try to make a more deep review.
Create one table for clients, first field called id_(table name), type Int, as primary key and with auto-increment extra feature. Then, when you add a record, a new auto-incremented unique id number will be generated for each client. That's the normal usage for tables.
You can translate the client id to another tables to maintain the relationships between tables (e.g. for posts table, I suggest to create the first field id_post, the second as id_client and then the additional ones - title, body, etc.).
When you add a new client the question is how to insert the resultant client id in another table? Well, you could do a lookup by client name to get the client id before, using a input field or a dropdown list with client names and id values, or use
same as mentioned by digital-ether again, in this case to get the latest inserted id from clients. When you make a insertion in another table do not forget to insert the client id.
You could make a lookup by logon name, e.g.
To make a single query through several tables with a same field in common (e.g. client id) you could perform a query like this:
That statement will display results from two tables like it was a single table.
Or you could do simple requests into each table and merge the resultants arrays in one.
Create one table for clients, first field called id_(table name), type Int, as primary key and with auto-increment extra feature. Then, when you add a record, a new auto-incremented unique id number will be generated for each client. That's the normal usage for tables.
You can translate the client id to another tables to maintain the relationships between tables (e.g. for posts table, I suggest to create the first field id_post, the second as id_client and then the additional ones - title, body, etc.).
When you add a new client the question is how to insert the resultant client id in another table? Well, you could do a lookup by client name to get the client id before, using a input field or a dropdown list with client names and id values, or use
PHP Syntax (Toggle Plain Text)
SELECT id FROM clients ORDER BY id_client DESC LIMIT 1
You could make a lookup by logon name, e.g.
PHP Syntax (Toggle Plain Text)
SELECT id FROM clients WHERE username = $logon_name
To make a single query through several tables with a same field in common (e.g. client id) you could perform a query like this:
PHP Syntax (Toggle Plain Text)
SELECT * FROM clients INNER JOIN posts ON clients.id_client = posts.id_client WHERE clients.name LIKE 'john smith'
That statement will display results from two tables like it was a single table.
Or you could do simple requests into each table and merge the resultants arrays in one.
Last edited by martin5211; Dec 5th, 2007 at 1:18 pm.
Digital-ether and martin5211, thanks for being patient and spelling it out for me. I think I'm starting to get a hang of it. Maybe I should start trying to make some tables and play with them. For the client table, do I only have the client names and unique ids? Or could I have other info in there as well, such as username?
![]() |
Similar Threads
- Hi, My Name Is Nubski (Visual Basic 4 / 5 / 6)
- php drop down menu to search multiple sql tables (PHP)
- Javascript/CSS Problem: Display is not properly hiding items (style.display="none") (JavaScript / DHTML / AJAX)
- IE works fine but Firefox doesn't display tables! (HTML and CSS)
- Why Use XHTML? (HTML and CSS)
- Large and tables/cells..how? (HTML and CSS)
- 200 GB Hard Drive Problems (Windows NT / 2000 / XP)
- C++ Relcat/Attrcat catalog help (C++)
Other Threads in the PHP Forum
- Previous Thread: js problem
- Next Thread: Sending a form via sendmail
Views: 2108 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date directory display download dynamic echo email error file files folder form format forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






