Foreign Restraint Keys

  1. How to link admins in the admin table to customers in the customer table
  2. How to limit admin authority to alter, view only records associated with the customer to whom the admins are attached
  3. The administrator could log in, add/alter a product in the product table using a php form. I would like to know how to ‘auto load’ certain product info based on the customer it belongs to, such as city, state, zip, phone number, email address, web address—which would be the same for all the products that a certain customer ‘owns’ in the storefront database products table. These products could be identified by their customer id, linked through foreign restraint key to the customer in the customer table and therefore to the admin in the admin table. Now the question, how to do it?

DATABASE IS BUILD, STOREFRONT IS BUILT, TABLES ARE BUILT.
MOST OF THE WEBSITE CODE IS COMPLETE.

SPECIFICALLY, I NEED TO KNOW HOW TO WRITE THE PHP/MYSQL:

  1. HOW TO ASSOCIATE AN ADMIN IN ADMINS TABLE WITH A CUSTOMER IN THE CUSTOMERS TABLE
  2. LIMIT WHAT ADMIN CAN SELECT, VIEW, ALTER TO THOSE PRODUCTS IN THE PRODUCTS TABLE THAT ARE ‘OWNED’ BY ASSOCIATED CUSTOMER IN THE CUSTOMER TABLE THAT SAID ADMIN IS ASSOCIATED WITH
  3. HOW TO ADD PRODUCTS TO THE PRODUCTS TABLE AND HAVE CERTAIN ITEMS FROM THE CUSTOMER TABLE AUTOMATICALLY INSERTED, SUCH AS CUSTOMER WEB ADDRESS, PHYSICAL ADDRESS, ETC.
  4. THIS INFO WOULD BE BASED ON THE ADMIN WHO IS ADDING OR EDITING THE PRODUCT, BASED ON THE ADMIN BEING ASSOCIATED WITH A CERTAIN CUSTOMER IN THE CUSTOMER TABLE

YOU WOULD NOT OWN THE CODE NOR RIGHTS TO IT NOR ANYTHING IN OR ABOUT THE WEBSITE. YOU WOULD SIMPLY BE EXPLAINING TO ME HOW TO UNDERSTAND AND WRITE SAID PHP/MYSQL INSTRUCTIONS TO MAKE THIS HAPPEN. THIS WOULD BE ACHIEVED VIA EMAIL AND SAMPLES OR IN A FORUM SETTING. YOU WOULD NOT ACCESS THE SITE NOR ACTUALLY WRITE THE CODE. MANY THANKS FOR ANY INSIGHT.

See sample code below that is INSUFFICIENT:

This is the new product code, including form for adding product data. It is missing the ‘hidden’ data I describe such as customer website, address, etc. based on the admin who is logged in and adding the product.

[php]<?php
if (!isset($_SESSION[‘store_admin’]))
{
echo “

Sorry, you have not logged into the system

\n”;
echo “<a href=“admin.php”>Please login\n”;
} else
{
$userid = $_SESSION[‘store_admin’];
echo “<form enctype=“multipart/form-data” action=“admin.php” method=“post”>\n”;
echo “

Enter the new product information


\n”;
echo “<table width=“100%” cellpadding=“1” border=“1”>\n”;
echo “ Category\n”;
echo “<select name=“catid”>\n”;

//Categories are decided by master admin and selected from a drop down menu

$query=“SELECT catid,name from categories”;
$result=mysql_query($query);
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$catid = $row[‘catid’];
$name = $row[‘name’];
echo “<option value=”$catid">$name\n";
}
echo “

\n”;

echo “

Customer ID Number <input type=“text” size=“40” name=“custid”> \n”;
echo “ Price <input type=“text” size=“10” name=“price”> \n”;

echo “

Type <input type=“text” size=“100” name=“foodtype”> \n”;
echo “ Item Name <input type=“text” size=“100” name=“entreename”> \n”;

echo “

Description <input type=“text” size=“40” name=“description”> \n”;
echo “ Price <input type=“text” size=“10” name=“price”> \n”;

echo “

Customer Name <input type=“text” size=“100” name=“restname”> \n”;
echo “ Customer Web <input type=“text” size=“100” name=“restweb”> \n”;

echo “<input type=“hidden” name=“MAX_FILE_SIZE” value=“1024000”>\n”;
echo “

Picture <input type=“file” name=“picture”> \n”;
echo “\n”;
echo “<input type=“submit” value=“Submit”>\n”;
echo “<input type=“hidden” name=“content” value=“addproduct”>\n”;
echo “\n”;
}
?>
[/php]
This is similar to the add product code, that I have considered adding the hidden data to, but don’t know how. I can’t write the multiple querries correctly and wonder if that is even the best way. The actual form and database table columns may not match, but I’m more worried about the theory and correct syntax rather than, for example, custphone is not lining up with custphone in the html form.

[php]<?php
$catid=$_POST[‘catid’];
$description=$_POST[‘description’];
$price=$_POST[‘price’];
$quantity=$_POST[‘quantity’];
$restname=$_POST[‘custname’];
$restphone=$_POST[‘custphone’];

if (get_magic_quotes_gpc())
{
$catid = stripslashes($catid);
$description = stripslashes($description);
$price = stripslashes($price);
$quantity = stripslashes($quantity);
$restname = stripslashes($custname);
$restname = stripslashes($custphone);

}
$catid = mysql_real_escape_string($catid);
$description = mysql_real_escape_string($description);
$price = mysql_real_escape_string($price);
$quantity = mysql_real_escape_string($quantity);
$restname = mysql_real_escape_string($custname);
$restphone = mysql_real_escape_string($custphone);

$thumbnail = getThumb($_FILES[‘picture’]);
$thumbnail = mysql_real_escape_string($thumbnail);

$query = “INSERT INTO products (catid, description, picture, price, quantity, custname, custphone) " .
" VALUES (’$catid’,’$description’,’$thumbnail’, ‘$price’, ‘$quantity’, ‘$custname’, '$custphone)”;

$result = mysql_query($query) or die(‘Unable to add product’);
if ($result)
echo “

New product added

\n”;
else
echo “

Problem adding new product

\n”;
?>
[/php]

THANKS!

Couldn’t you just add an ADMIN field to all products. Then, when someone logs in, use the new field to limit what products he can view or alter. Not sure if that is what you are asking. Once the ADMIN selected Products are used in the query to pull a list of products, the admin would only have the ones he is allowed to use. As far as what he can change, that should be listed in his profile. The profile would have areas allowed. So the combination of the two would take care of your needs.

Sorry, this is just general ideas to start up this thread some…

Thank you. I could try that…

I’m having trouble limiting what the admins can do in phpmyadmin. Can’t seem to limit them to alter only the items in the ‘products’ table that they ‘own’. I know how it should work, but seems like phpmyadmin won’t do it or I don’t know how.

As far as adding the admin field, I did that today. I just didn’t know it would be as simple as you say.

There may be more than one admin for each customer, so how would the ‘add an admin field’ work in that case? I don’t want it to be so complicated that field sales reps can’t add an admin without hard coding the admin’s admin number in the admin field, for example. Thanks.

Also, when I add the admin key, how would I write the ‘double query’ that would be something like:

SELECT products FROM (the products table) WHERE admin = ? admin code (but this is in the admin table) and would require selecting the admin data from admin table and products from the products table.

when I assign with $rows, I get errors with various items being ‘undefined’

That is the problem I’m having. The query and query2 don’t seem to work too well for me when I try. Thanks.

Well, I would not use “phpmyadmin”. That is a canned program that is supposed to be used just by the ADMIN of the entire site, not to put out in the public…

Usually, you create a webform that admins would log into and that form would contain whatever they are allowed to administer. Usually, one admin “user” would only see his data he controls. So, things like database stored keys or stored routines would never been seen. You can NOT protect it completely if you allow someone to use PHPmyAdmin. Well, actually you can by writing your own version of it. Is that what you did? It is fairly easy to create your own version. Use SESSION variables to store the logged in admin’s userID and make sure whenever the pages change that you check it and only show his info…

Also, there is a trick for multiple records stored into one database record. So, using this trick, you could use one admin field with several admin names stored in it. When someone is added to the field, add their userID at the end of the current value of the field. Here is an example: (not real code, just the basics!)

$newAdmin=“SuperErnie”;
$AdminField=$AdimField+"|"+$newAdmin;

What this does does is add a second one to the end of the first one with a bar for separation.
When, you check someone to see what they are allowed, you can pull all the names into an array.
Then, use inarray() to test if the userID is in the list… It works, just takes a lot of thinking ahead.
Or, if you are good with array, just store the array into the field and make sure to do the SQL so it
checks the content of the field for the userID… A little tricky, but, doable!

So, some things for you to think about… Hope that helps… (PS: bed soon!)

Sponsor our Newsletter | Privacy Policy | Terms of Service