hance2105 Posted June 22, 2013 Share Posted June 22, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279460-add-products-to-database-by-user_id/ Share on other sites More sharing options...
Barand Posted June 22, 2013 Share Posted June 22, 2013 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. Don't you already have that in your retailer table (id, user_id, prod_id)? Quote Link to comment https://forums.phpfreaks.com/topic/279460-add-products-to-database-by-user_id/#findComment-1437442 Share on other sites More sharing options...
hance2105 Posted June 23, 2013 Author Share Posted June 23, 2013 i just created the table i dnt know how to code it now Quote Link to comment https://forums.phpfreaks.com/topic/279460-add-products-to-database-by-user_id/#findComment-1437503 Share on other sites More sharing options...
hance2105 Posted June 23, 2013 Author Share Posted June 23, 2013 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. <?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. <?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.... Quote Link to comment https://forums.phpfreaks.com/topic/279460-add-products-to-database-by-user_id/#findComment-1437514 Share on other sites More sharing options...
Solution mac_gyver Posted June 23, 2013 Solution Share Posted June 23, 2013 programming involves defining the steps, conditions, and data that you need to perform any task. what inputs do you have, what processing do you want to perform on those inputs, and what result or output do you want. you start with the general statement of what you want to do and then continue to break the problem down into smaller and more detailed steps. finally, you write the code that performs each of those steps. after checking if the current logged in visitor is a retailer and can even perform this operation, wouldn't you get the user_id from your login code and the submitted prod_id(s) from a form submission and insert the data into the retailer table (after validating it), taking into account that there isn't already a row with the same user_id/prod_id in the table? if you are going to insert multiple rows at one time, it is much more efficient to form a multi-value insert query and run one query to insert all the rows at one time. the syntax for this can be found in the mysql INSERT documentation. what have you defined for the user interface? does it list and have a way of selecting product(s) that have not already been selected by the current user? does it check if the current logged in visitor is a retailer and can even perform this operation? Quote Link to comment https://forums.phpfreaks.com/topic/279460-add-products-to-database-by-user_id/#findComment-1437515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.