add products to database by user_id
Moderator: General Moderators
add products to database by user_id
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
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
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add products to database by user_id
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.hance wrote:added to the database for every product that is added by him
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: add products to database by user_id
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.
i want to create smthng similar but for retailers who are adding products. below is the code to add products which i already created.
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....
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);
?>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>' ;
}
?>hope this can help....
Last edited by requinix on Mon Jun 24, 2013 3:30 pm, edited 1 time in total.
Reason: [php] tags don't work well. please use [syntax=php][/syntax]
Reason: [php] tags don't work well. please use [syntax=php][/syntax]
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add products to database by user_id
how do you store the user_id of the retailer after they have logged in?hance wrote:i would like the user_id of the retailer
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: add products to database by user_id
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...
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add products to database by user_id
i see, is it assigned as a session variable? Here is what i'm thinking:hance wrote:it is stored as an integer
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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: add products to database by user_id
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.
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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add products to database by user_id
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')");
?>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')");
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering