Page 1 of 1
add products to database by user_id
Posted: Sat Jun 22, 2013 2:17 pm
by hance
i have a table tblretailer and a tbllogin. when a retailer registers a user_id is generated in the tbllogin and role of 'retailer' assigned.
when a retailer logs in, his session starts. he can add, delete or update products. i have the codes to add, delete and update products.
several retailers can add, delete or update different and same products.
i created a table called retailer.php which stores the user id and the prod_id that was added by the retailer, and an auto_increment id.
retailer.php - id, user_id, prod_id
tbl_product - prod_id, prod_name, prod_desc, prod_price, prod_photo
tbllogin - user_id, username, password, role
i would like the user_id of the retailer to be added to the database for every product that is added by him. this way i can perform queries by retailer.
any idea how i can do that?
let me know if the codes i already wrote will be of some use to help me out please
Re: add products to database by user_id
Posted: Sat Jun 22, 2013 3:21 pm
by social_experiment
hance wrote:added to the database for every product that is added by him
are you storing the user id of a retailer in a session variable? Once a retails adds a product you can take that variable and write it to the database along with the other relevant information.
Re: add products to database by user_id
Posted: Sun Jun 23, 2013 11:04 am
by hance
i created this piece of code for users adding products as favourites and it is being stored in a tblfavorites table along with the user id from tbllogin and product id from tbl_product. this works great.
Code: Select all
<?php
session_start();
include "db_connect.php";
$username = $_SESSION['username'];
$sql=mysql_query("select user_id from tbllogin where username = '$username'");
$row=mysql_fetch_array($sql);
$sql1 = mysql_query("select a.prod_id, a.prod_name, a.prod_photo from tbl_product a, tblfavourites b where a.prod_id=b.prod_id AND b.user_id='$row[user_id]'");
$row1=mysql_fetch_array($sql1);
?>
i want to create smthng similar but for retailers who are adding products. below is the code to add products which i already created.
Code: Select all
<?php
if(isset($_POST['button'])){
include('db_connect.php');
$prod_name=$_POST['prod_name'];
$prod_brand=$_POST['prod_brand'];
$prod_desc=$_POST['prod_desc'];
$prod_price=$_POST['prod_price'];
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
$prod_w_c=$_POST['prod_w_c'];
$target = "Images\Products/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$file = ($_FILES['uploaded']['name']);
$ok=1;
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file " . basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
$sql=mysql_query("INSERT INTO tbl_product(prod_name, prod_brand, prod_desc, prod_price, cat, subcat, prod_w_c, prod_photo) VALUES('$prod_name', '$prod_brand', '$prod_desc', '$prod_price', '$cat', '$subcat', '$prod_w_c', '$file')");
$sql=mysql_query("select prod_photo from tbl_product where prod_name='$prod_name'");
$row = mysql_fetch_array($sql);
$x = $row['0'];
echo $x;
echo '<li><a href="Images\Products/'.$x.'"><u>Click here to download original Contract</u></a><li>' ;
}
?>
the user_id of the retailer is being stored in a tbllogin....the user_id is auto incremental in the table tbllogin which has the following fields - user_id, username, password, role
hope this can help....
Re: add products to database by user_id
Posted: Sun Jun 23, 2013 11:59 am
by social_experiment
hance wrote:i would like the user_id of the retailer
how do you store the user_id of the retailer after they have logged in?
Re: add products to database by user_id
Posted: Sun Jun 23, 2013 12:26 pm
by hance
it is stored as an integer. the user_id is auto_incremental. each time a customer or retailer is registered a user_id is assigned to them in the login table...
Re: add products to database by user_id
Posted: Sun Jun 23, 2013 3:43 pm
by social_experiment
hance wrote:it is stored as an integer
i see, is it assigned as a session variable? Here is what i'm thinking:
Code: Select all
// user login, session variables set (one of the sess. variables are the retailer id) $_SESSION['retailer_id'];
// retail adds new product, insert $_SESSION['retailer_id'] value into appropriate query to be
// written to the database
Re: add products to database by user_id
Posted: Mon Jun 24, 2013 4:03 am
by hance
the first piece of code is the one i created for users who are setting products as favourites. it is stored in a tbl favourites along with their user ids stored in tbl login.
it works great and each user after loggin in can see their favourites without any issue.
i want the same thing but for retailers, where when they add products their user id is displayed in the tblproduct for products they added. even if several retailers add the same product it can be differentiated with the user id.
am not knowing what lines of code i should add to the second code i provided to be able to do so.
Re: add products to database by user_id
Posted: Tue Jun 25, 2013 4:20 pm
by social_experiment
Code: Select all
<?php
$sql=mysql_query("INSERT INTO tbl_product(prod_name, prod_brand, prod_desc, prod_price, cat, subcat, prod_w_c, prod_photo) VALUES('$prod_name', '$prod_brand', '$prod_desc', '$prod_price', '$cat', '$subcat', '$prod_w_c', '$file')");
?>
the query that inserts the data into the table has to be modified; you need to add the user_id value for each retailer (per product they enter), perhaps you even have to modify your tbl_product's structure to accommodate a new field (retailer_id)
Code: Select all
<?php
// new query could look like this
$sql=mysql_query("INSERT INTO tbl_product(prod_name, prod_brand, prod_desc, prod_price, cat, subcat, prod_w_c, prod_photo, retailer_id) VALUES('$prod_name', '$prod_brand', '$prod_desc', '$prod_price', '$cat', '$subcat', '$prod_w_c', '$file', '$retailerIDValue')");
?>