Jump to content

Differentiate between products for E-commerce site from database


louiscb

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

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>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.