Link to home
Start Free TrialLog in
Avatar of kevandju
kevandjuFlag for United States of America

asked on

User control using mysql query in PHP

I have 200 products, each user only has control of certain products that they can edit.  I have 3 tables in my database.

I have a table for "users" that has the user_id (auto incrementing number), username, password and email.

Next table is "users_products" that has their up_user_id number and then up_product_id number.

Then I have products which has product_id, name, price.

I store their user id in a session.

Through the URL is how I pass my variables to each page.  So for instance http://domain.com/edit_product.php?id=7

I want to make it so on edit_product.php that they can only edit product_ids that are listed with their user_id in "users_products".  Or else they can simply manipulate the URL and change the id to some product that they aren't allowed to edit.

This is the query I use on the "view_products.php" page so they only see their products

SELECT product_id, name, price FROM users_products, products WHERE product_id=up_product_id AND up_user_id='20' GROUP BY product_id

Avatar of smantscheff
smantscheff

So what is exactly your question? Use a similar query in edit_product.php:
SELECT products.* FROM users_products, products
WHERE product_id='$productId'
AND product_id=up_product_id
AND up_user_id='$userId'
Avatar of kevandju

ASKER

My question is that I need to validate that the user is allowed to edit the product id or else take them back to view_products.php.  I don't want someone to edit the URL and start editing products that aren't authorized to edit.
Avatar of Armand G
Hi,

Use this query to check if they can edit:

SELECT product_id, name, price FROM users_products, products WHERE product_id=up_product_id AND up_user_id='20' AND product_id IN (SELECT users_products WHERE up_user_id='20') GROUP BY product_id
Sorry, this should be the correct one:

SELECT product_id, name, price FROM users_products, products WHERE product_id=up_product_id AND up_user_id='20' AND product_id IN (SELECT up_product_id FROM users_products WHERE up_user_id='20') GROUP BY product_id
Do your users sign in with password authentication?  If so, you've got the user id.  And with the user id you can make a SELECT against the users_products table.  If the thing they want to edit matches, you're OK.  If there are no rows in the results set, they cannot make the edit.  I'm guessing that the id= string in the URL GET argument is the id of the products, right?
Yes they sign in with password identification and the user_id is stored in their session so I have access to that.

I"m thinking I need to run a query to see if they user_id is allowed to see that product_id and if results of the query returns 0 rows then I will just redirect them back to view_products.php.
I suggest generating a fairly long random string and setting this as a session cookie whenever they log in. Compare the string from the cookie with the string in temporary session storage on the server, and bam your user is authenticated. Then it will be impractical for users to guess this long string. If you can't use cookies, compare ip address with $ip = getenv("REMOTE_ADDR") ;

http://www.w3schools.com/php/func_http_setcookie.asp

ASKER CERTIFIED SOLUTION
Avatar of kevandju
kevandju
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looking at this comment from ID:34177714
"... if results of the query returns 0 rows then I will just redirect them back to view_products.php."

Here is how I might design it.  For every product that is displayed on the view_products page check the users_products table.  If they are allowed to update the product, you can add a link that says, "Update this" and you can omit that link from the other product displays.  You still have to perform server-side validation of any update requests, but your site will make sense to all the visitors because they will only see "Update this" links on the products they control.

A little bit of clever query design can make this very efficient.  Perhaps one query gets all the rows for all the products and a second query gets all the rows that the client controls.  Then as you produce the HTML, you can test with in_array() to see if the product should get the "Update this" link.
I already do this. The view products page only shows their products. I was trying to avoid people manipulating the URL to edit products that weren't theirs.
Then just check the users_products table whenever an update request is received and only proceed if there is a match between user and product.  It should work great.
Was able to come up with the query and solution on my own for this problem.