Jump to content

Recommended Posts

Hey Everyone,

I have this query that I am using to display some links. Basically I want the query to say select the info IF current = 1 and admin = 1, but in this table I am querying, I have 20 columns that are for users' id number and if the logged in user's number is in one of these columns, I should display too.  I can not get the right AND / OR mix to get it to do what I need.  Is it clear what I am looking for and does anyone have any suggestions?

 

SELECT * FROM projects WHERE current = '1' AND admin = '1' AND user1=$user_id OR user2=$user_id OR user3=$user_id OR user4=$user_id OR user5=$user_id OR user6=$user_id OR user7=$user_id OR user8=$user_id OR user9=$user_id OR user10=$user_id OR user11=$user_id OR user12=$user_id OR user13=$user_id OR user14=$user_id OR user15=$user_id OR user16=$user_id OR user17=$user_id OR user18=$user_id OR user19=$user_id OR user20=$user_id

Link to comment
https://forums.phpfreaks.com/topic/86676-help-with-query/
Share on other sites

There are two problems with the query that you showed. I'll start with the easier issue.  Whenever you have AND and OR operations, the OR operations should ALWAYS be surrounded by parentheses () because of precedence.  So, in your query, I would do something like this:

 

SELECT * FROM projects WHERE current = '1' AND admin = '1' AND (user1=$user_id OR user2=$user_id OR user3=$user_id OR user4=$user_id OR user5=$user_id OR user6=$user_id OR user7=$user_id OR user8=$user_id OR user9=$user_id OR user10=$user_id OR user11=$user_id OR user12=$user_id OR user13=$user_id OR user14=$user_id OR user15=$user_id OR user16=$user_id OR user17=$user_id OR user18=$user_id OR user19=$user_id OR user20=$user_id)

 

If you omit the parens, the ADMIN='1' AND user1=$user_id would get evaluated first, and I'm pretty sure that's not what you want.  Remember, AND always takes precedence over OR.

 

The second problem is a design issue.  I'm not really sure why you have user1, user2, user3 columns.  If you have multiple users, what you should really have is a separate table that holds all the user IDs, like this:

 

USERS

user_id (primary key)

name

...

 

 

and in your PROJECTS table, have an foreign ID column that references the USERS user_id column, like this:

PROJECTS

current

admin

user_id (foreign key to USERS)

 

 

Then you query would look like this:

SELECT * FROM projects, users WHERE current='1' AND admin='1' and projects.user_id=users.user_id;

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/86676-help-with-query/#findComment-442961
Share on other sites

Yes, the way you do that is with yet another table.  Suppose we originally have a USER table, PROJECTS table, and a new table called USER_PROJ_REL table.  The USER_PROJ_REL table will tell us which users belong to which projects by assigning a project ID from the PROJECT table along with a user ID from the USERS table into a new row.

 

So, each project has a unique ID (unique relative to the PROJECT table), each USER has a unique ID, and the USER_PROJ_REL will contain a relation between the two:

 

USERS

user_id (primary key)

...

 

PROJECTS

project_id (primary key)

admin

content

 

 

USER_PROJ_REL

user_id (foreign key to USERS table)

project_id (foreign key to PROJECTS table)

 

 

Suppose you have users with IDs 1,2,3 belonging to a project with ID 999. The USERS_PROJ_REL table would look like this:

USER_ID, PROJECT_ID

1        999

2        999

3        999

 

So you query would then look like this:

 

SELECT projects.*, users.* FROM users, projects, user_proj_rel WHERE admin='1' AND content='1' AND projects.project_id = users_proj_rel.project_id AND users.user_id = user_proj_rel.user_id AND projects.project_id='999';

 

This will give you a list of all users belonging to project 999.

Link to comment
https://forums.phpfreaks.com/topic/86676-help-with-query/#findComment-443024
Share on other sites

Okay I 100% get how this works and see the reasoning behind it, but how can I insert data into two tables at once.  I have a form where users fill out the project info, and who is assigned to the project, but I do not want to have two forms where two seperate INSERT queries are made.  What is the structure for making two inserts at once?

Link to comment
https://forums.phpfreaks.com/topic/86676-help-with-query/#findComment-443152
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.