Jump to content

Differentiate between products for E-commerce site from database


Go to solution Solved by Ch0cu3r,

Recommended Posts

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 :)

  • Solution

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 by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.