How do I allow certain users access to certain records?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

How do I allow certain users access to certain records?

Post by tristanhall »

I want to build a site that lets users manage their own receipts, expenses and incidents. However I also want this to handle more than one user. I want each user to only be able to see the receipts that they have entered and the expenses and incidents that only they entered. How would I go about doing this? My first thought was a database for each user but that just isn't practical. I would also have one admin user that can see everything. I would want to set it up so that receipts of each user would be added up and a total for each user would be echoed to the page. Any tips? My biggest struggle is having each user only able to view their own info. Thanks in advance!
-Tristan
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I allow certain users access to certain records?

Post by Christopher »

You can use one database, just add the User ID as a field in the receipts, expenses and incidents tables. Then fetch records with "SELECT * FROM receipts WHERE user_id='$userid'". You would set the value for $userid in the session when the user logs-in, and then get it from the session when you need to fetch records.
(#10850)
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Re: How do I allow certain users access to certain records?

Post by tristanhall »

that is GENIUS!! I can't believe i didn't think about that. thanks a million!
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Re: How do I allow certain users access to certain records?

Post by tristanhall »

one more thing. how would i use PHP to display the sum of all the records in a certain field? Like if a person filled in a bunch of receipts I would want the sum of all the numbers in the "total" field of the database to be displayed underneath my table that displays the receipts. Kind of like an Excel sheet where you can have the sum of a certain set of cells automatically update when a value of a cell is changed. I'll have a varying number of cells though.
Example:
Receipt 1 --- $34.56
Receipt 2 --- $4.45
Receipt 3 --- $5.34
Receipt 4 --- $35.23
Total --- $(SUM of all Receipts)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I allow certain users access to certain records?

Post by Christopher »

You can use PHP to loop through all of the fetched records to get a total, or use the SQL SUM() function.
(#10850)
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Re: How do I allow certain users access to certain records?

Post by tristanhall »

thank you very much for your help.
Post Reply