user permissions - mysql

im doing project for a school...and new to php and mysql..just learning..
i have to create one loginpage with 3 different users like corres,principal and the staff
in the table field what type i have to give like ENUM or SET..
i want to assign 0 for corr,1 for princi and 3 for staff..
how to do this and shud i give any field in the login page in php??
but im going to create only 2 tables one for corr and other one is for staff
corr table fields r corrs_id,username,password,staff_id,staff_role
staff table fields r staff_id,staff_role,name,qualification,address,state etc...
both the tables have 2 common fields staff_id and staff_role
now how to connect 2 tables and when username and password is given it shud first check the staff_role like 'princ' or a 'staff'
please help me....

Don't keep two same fields in 2 tables if you can get rid of one. Read about database normalization first.
Remove the staff_role field from the corr table and leave only the staff_id to join both tables as I said above.
The two tables are joinable using this exact staff_id
select u.name,r.role from users u
inner join roles r
on u.role_id=r.id
where r.role='princ' and u.username='bleh' and u.password='bleh'
Cheers

Related

How to show username if i have 2 tables, user and review

i am creating a restaurant review website. in my review table i have a foreign key called user_id and idk how to use it to display the username which is in the user table
my user table
my review table
so my question is how do i display the username from this? what mysql statement do i have to write. I am lost on what to do
Assuming you want to try and get the review text along with the user name from the corresponding user you can use a join to combine the info for example:
SELECT u.username, r.review_text
FROM reviews r
LEFT JOIN users u
ON (u.user_id = r.user_id)
I assumed the users table is called users and reviews table is called reviews but update those as necessary each is "aliased" as u and r respectively and then tables are joined
If the relationship between the two tables is mapped out correctly you should be able to run a query to fetch the name of each user. Try to avoid any N+1 query though

How to avoid table for each user

I have a rather special use case in front of me. There is to be an excel file with around a thousand entries (rows), each row represents something that the USER should pass judgment on.
Now, the entries are the same for everyone. The data that should be collected is
a) how many users like any given entry
b) what entries does any given user like
Since part of the app is already running and we have user accounts,
I thought of creating a table for each user (!) containing said excel information, adding a row for collecting the votes. I would create those tables by iteratin through the user list and creating tables like "userid_excelentries".
I don't think that's elegant. I would prefer to store the excel information only once in a table and only save the users' votes in the table "user".
The app is meant to display a table created form the excel table (I have the grid already done) and a row next to it with checkboxes. How do I structure this ? Temporary tables ? How do I store the information what each user has selected in the "user" table, since I don't know how many selections will be made a-priori ?
I had this crazy idea of actually handling the xls object through javascript, serializing it into a hash and storing that hash into a field in each user's row...but I have no clue if this is sane :o
We're facing a user count of exactly 272 - this is why I considered doing the "one table for each user" approach.
You can use 3 tables in your DB
users table
-----------
id
name
...
entries table
-------------
id
name
...
user_entries table
------------------
user_id
entry_id
user_response
To get all entries a certain user (i.e. Tom) likes you can do
select e.name
from entries e
join user_entries ue on ue.entry_id = e.id
join users u on ue.user_id = u.id
where u.name = 'tom'
and ue.user_response = 'like'
And to get the count of likes for each entry you can do
select e.name, count(ue.user_id) as likes
from entries e
join user_entries ue on ue.entry_id = e.id
where ue.user_response = 'like'
group by e.id, e.name

trying to link address table with companyX

I have question related to mysql relationships. Let's say I have 4 table companya companyb companyc and companyd, each containing 3 columns: name, phone, and age. Also I have another table called address which I want to link to each companyX with this address table , let’s say if I did select * from companyX; the address column should appearnext to each name.
Can this be done with mysql?
First of all, if the company tables have the exact same structure why don't you use only a single table to store the information? You may want to read about database normalization.
Secondly you could create a relation table named something like 'companiesAddresses' and store fields such as "CompanyID" and "AddressID" in it (you obviously would have to create a primary key column in your companyX's table and do the same in the 'addresses' table so then you could use the INNER JOIN clause to get the information you want such as SELECT companyX.companyName, addresses.Address FROM companyX INNER JOIN companiesAddresses ON companyX.CompanyID = companiesAddresses.CompanyID INNER JOIN addresses ON companiesAddresses.AddressID = addresses.AddressID.
I hope I understood what you want correctly.

MYSQL: should i use 2 tables for 2 types of user with common and different field?

i have 2 type of users
1. employee.
2. employees friend.
both users have common fields.
1.first name
2.middle name
3.surname
4.cellphone number
5.telephone number
6.city address
7.email (serve as login)
8.password
9.registration date
10. update account date
and the employee friend user don't have this field
1.employee id
2.company name.
3.company branch
4.position
and they will be going to use same login form.
edited version (thanks to nawfal and Patrick James McDougle)
1st table (user) should contain field 1-10(first list) and a new field fk_employees (foreign key to our 2nd table, null if it is an employees_friend)
2nd table (employees) should contain field 1-4(second list)
I think about this the same way I think about object oriented programming. If employee EXTENDS user then employee should be a user with extra information. I would have two tables.
Users (containing the common fields adding an id field perhaps)
&
Employees (with the 4 extra fields for employees and one field that references a unique identifier in the users table.)
More information about what I am proposing can be found here: http://www.agiledata.org/essays/mappingObjects.html#MappingInheritance
what i suggest create 2 separate tables as users and user_friends with the fields mentioned above.
Now create a view with following query:
select employeeID,email,password from users
union
select friendID,email,password from user_friends
Query this view to get the login info in your application.

SQL Query/Procedure with user input to view all persons meeting a criteria

I have an SQL database which I need to create a procedure or query that is linked to a web front end where the user can type in a value which will be in one of the columns of the database. The user types in the value, submits the command (just normal text entry in a textbox in the website), a query is ran in the background to view all persons who have this value in the column under their entry.
User entry to determine the outcome for a view of specific people.
SQL Side I have currently but not certain it is correct, works but not as intended!
I have a table with people in it and a model of interest value column.
Each model of interest is just a number 1-7 which relates to a product range in another table. So 1 = a Corsa, 2 = Astra, 3 = Vectra etc.
So in the persons table, they will have purely a 1 or a 2 etc. Now I need it to be linked to the model table so if it sees 1 it looks for Corsa rather than the number 1.
A user types in the frontend, select all users interested in a Corsa, it will look up the word corsa and match it to the value 1, then search for 1 in the model of interest column in the person table.
So far I have the following query. Any suggestions as I'm stumped for today. Will try again tomorrow.
SELECT TOP 100 [ld_idno]
,[ld_company]
,[ld_decisionmaker]
,[ld_decisionmaker_workphone]
,[ld_decisionmaker_mobile]
,[ld_decisionmaker_email]
,[ld_discussion_model]
FROM [FMLive204].[dbo].[tblLeads]
SELECT a.po_word
FROM dbo.tblPopulation a
INNER JOIN dbo.tblLeads b
ON ld_discussion_model = po_idno
WHERE [po_word]='Corsa'
SELECT TOP 100 Unique Records
company name
customer name
customer workphone]
customer mobile]
customer email]
[ld_discussion_model] Model of interest = 1
FROM [FMLive204].[dbo].[tblLeads]
SELECT a.po_word
FROM dbo.tblPopulation a (table a)
INNER JOIN dbo.tblLeads b (table b)
ON ld_discussion_model = po_idno (if the model of interest 1 is the same as an entry in the other table, pick the value in the next columne (po_word)
WHERE [po_word]='Corsa'
Daniel,
Below I've outlined the stored procedure which would require the userdata (e.g. Corsa entered from the website) and return all fields from tblpopulation where 'po_idno' is in the list matched against the tblLeads. Not having the tblpopulation definition I'm guessing for field name 'po_idno' is the field which has the value 1-7 in it.
Create PROCEDURE [dbo].[sp_find_person] (#UserData varchar(6))
AS
BEGIN
SELECT Top 100 * FROM [FMLive204].[dbo].tblpopulation
where po_idno in (SELECT po_idno FROM dbo.tblLeads b
JOIN dbo.tblPopulation a
ON ld_discussion_model = po_idno
WHERE [po_word]=#UserData)
Once you create the procedure you'll have to execute it which more information can be found # http://msdn.microsoft.com/en-us/library/ms189915.aspx