louiscb Posted November 30, 2014 Share Posted November 30, 2014 I am building an e-commerce site and I am aiming to create a front end displaying my products with an option for customers to buy them, and have a content management system as a back end for an admin to edit product information. Currently I am storing information about my products on a mysql database. I access and display the product info using a while loop. Below is a simplified version of what I am doing without any html to style it. This code will go through the database and each iteration will go the to the next row and display the info about the next product. $query = mysql_query("SELECT * FROM truffleProducts"); while ($row = mysql_fetch_array($query)) { $id = $row['id']; $name = $row{'Name'}; $price = $row{'Price'}; $desc = $row{'Description'}; echo $id; echo $name; echo $price; echo $desc; } I have began to implement a 'buy' button so that customers are able to click on a button next to the product info and purchase it. However I have come across a problem which is where my program won't be able to tell which product you have selected as the number stored in the $id variable will just be the last product it has fetched from the database. I can't differentiate between all the product's buy buttons, so it will impossible to place an order for a customer with the current system I have. Can any one tell me how to get the id number of the specific product that a user has selected? I only started learning PHP a month or two ago so assume I know nothing Quote Link to comment https://forums.phpfreaks.com/topic/292812-differentiate-between-products-for-e-commerce-site-from-database/ Share on other sites More sharing options...
Solution Ch0cu3r Posted November 30, 2014 Solution Share Posted November 30, 2014 (edited) You need to output a separate form foreach product which contains the buy button and product id in a hidden input field. When you click the buy button next to the product it will be the products id being submitted to your receiving script. This is how you'll identify which product the user chose Example code while ($row = mysql_fetch_array($query)) { $id = $row['id']; $name = $row{'Name'}; $price = $row{'Price'}; $desc = $row{'Description'}; echo $id; echo $name; echo $price; echo $desc; // out put buy button for product // product is in hidden input field echo ' <form action="checkout.php" method="post"> <input type="hidden" name="productId" value="'.$id.'" /> Qty: <input type="input" name="quantity" /> <input type="submit" name="buy" value="Buy" /> </form>'; } Edited November 30, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/292812-differentiate-between-products-for-e-commerce-site-from-database/#findComment-1498114 Share on other sites More sharing options...
louiscb Posted November 30, 2014 Author Share Posted November 30, 2014 @Ch0cu3r Thank you Quote Link to comment https://forums.phpfreaks.com/topic/292812-differentiate-between-products-for-e-commerce-site-from-database/#findComment-1498126 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.