Shopping Cart Tutorial PHP Shopping Cart Tutorial
Learn how to create an online store with PHP and MySQL
Free shopping cart    Download:
Shop-Script FREE
Shop-Script FREE User Guide (PDF; 0 Kb)
View Live Demo    View online store demo
 Introduction
 Basic concepts
 Technologies
 File structure
 Database structure
 Back end
   Password protection
   Products catalog: structure
   Product catalog: viewing
   Managing categories
   Managing products
   Special offers
   Managing orders
 Front end
   Viewing products catalog
   Product search
   Shopping cart
   Checkout
   PayPal integration
 Security
 Live Demo
 Author
 Terms Of Use

Shopping cart software

 

Adding / editing / deleting products

Managing products is even easier than managing categories, because it does not require making products hierarchical tree structure.
All products are plainly saved into separate PRODUCT table.

At-first, we have to develop a form for managing product details.
This form should allow managing product name, descriptions, images, etc.
This is how the form looks in Shop-Script FREE and its HTML code (products.php):

<form enctype="multipart/form-data" action="products.php" method="POST">

<table width="100%" border="0" cellpadding="3" cellspacing="0">

<tr>
<td align="right">Parent:</td>
<td>
<select name="categoryID">
<option value="0">Root</option>
<option value="4">Sample category 1</option>
<option value="20">  Child category 1</option>
<option value="76">  Child category 2</option>
< all categories are shown in this select box >
</select>
</td>
</tr>

<tr>
<td align="right">Product name</td>
<td><input type="text" name="name" value=""></td>
</tr>

<tr>
<td align="right">Product code</td>
<td><input type="text" name="product_code" value=""></td>
</tr>


<tr>
<td align="right">Price, USD<br>(number only):</td>
<td><input type="text" name="price" value=0></td>
</tr>

<tr>
<td align="right">List price, USD<br>(number only):</td>
<td><input type="text" name="list_price" value=0></td>
</tr>

<tr>
<td align="right">In stock:</td>
<td><input type="checkbox" name="in_stock" checked></td>
</tr>

<tr>
<td align="right">Picture</td>
<td><input type="file" name="picture"></td>
<tr><td></td><td>
(picture not uploaded)</td>
</tr>
<tr>
<td align="right">Thumbnail</td>
<td><input type="file" name="thumbnail"></td>
<tr><td></td><td>
(picture not uploaded)</td>
</tr>
<tr>
<td align="right">Enlarged picture</td>
<td valign="top"><input type="file" name="big_picture"></td>
<tr><td></td><td valign="top">
(picture not uploaded)</td>
</tr>


<tr>
<td align="right">Description<br>(HTML):</td>
<td><textarea name="description" rows="15" cols="40"></textarea></td>
</tr>

<tr>
<td align="right">Brief description<br>(HTML):</td>
<td><textarea name="brief_description" rows="7" cols="40"></textarea></td>
</tr>

</table>


<p><center>
<input type="submit" value="Save">
<input type="hidden" name="save_product" value="0">
<input type="button" value="Cancel" onClick="window.close();">
</center></p>
</form>

Adding product

Administrator is prompted to fill in the form presented above.
Clicking "Save" button will execute following SQL query to save product into the database (products.php):

INSERT INTO `PRODUCT` (categoryID, name, description, customers_rating, Price, in_stock, customer_votes, items_sold, enabled, brief_description, list_price, product_code) VALUES ('".$_POST["categoryID"]."','".$_POST["name"]."','".$_POST["description"]."', 0, '".$_POST["price"]."', ".$instock.", 0, 0, 1, '".$_POST["brief_description"]."', '".$_POST["list_price"]."', '".$_POST["product_code"]."');

Editing product details

Here administrator is prompted to fill in the same form as when adding a product.
The only difference is that in this case we pass productID along with the form to indicate that clicking "Save" button should update existing product details, but not add a new one (products.php):

<?php

    
if (!isset($_POST["price"]) || !$_POST["price"] || $_POST["price"] < 0)
        
$_POST["price"] = 0//price can not be negative

    
if (!isset($_POST["name"]) || trim($_POST["name"])==""$_POST["name"] = "not defined";

    
$instock = (isset($_POST["in_stock"])) ? 0;

    if (
$_POST["save_product"]) { //if $_POST["save_product"] != 0 then update item

        //delete old product photos if they're being replaced
        
$q db_query("SELECT picture, big_picture, thumbnail FROM ".PRODUCTS_TABLE." WHERE productID='".$_POST["save_product"]."'") or die (db_error());
        
$row db_fetch_row($q);

        
//generating query

        
$s "UPDATE ".PRODUCTS_TABLE." SET categoryID='".$_POST["categoryID"]."', name='".$_POST["name"]."', Price='".$_POST["price"]."', description='".$_POST["description"]."', in_stock=".$instock.", customers_rating='".$_POST["rating"]."', brief_description='".$_POST["brief_description"]."', list_price='".$_POST["list_price"]."', product_code='".$_POST["product_code"]."'";

        
$s1 "";

        
//old pictures?
        
if (isset($_FILES["picture"]) && $_FILES["picture"]["name"])
        {
            
//delete old picture
            
if ($row[0] && file_exists("./products_pictures/".$row[0]))
                
unlink("./products_pictures/".$row[0]);
        }
        if (isset(
$_FILES["big_picture"]) && $_FILES["big_picture"]["name"])
        {
            
//delete old picture
            
if ($row[1] && file_exists("./products_pictures/".$row[1]))
                
unlink("./products_pictures/".$row[1]);
        }
        if (isset(
$_FILES["thumbnail"]) && $_FILES["thumbnail"]["name"])
        {
            
//delete old picture
            
if ($row[2] && file_exists("./products_pictures/".$row[2]))
                
unlink("./products_pictures/".$row[2]);
        }

    }

?>

Deleting product

When clicking "Delete" button for a certain product it is simply deleted from the database using following SQL query:

DELETE FROM `PRODUCT` WHERE productID=`$productID_to_delete`;

 

 


Copyright © 2006 WebAsyst LLC