Jump to content

Shopping cart problem


desmond_ckl

Recommended Posts

Hi !! i need some serious help here.....this code make me crazy  :'(

 

recently i have follow a tutorial about shopping cart, there is  2php file inside this (seestore.php & showitem.php).

In this tutorial, i have create 4 table

i) store_categories (id, badminton_title, badminton_desc)

ii) store_items (id, badminton_id, item_title, item_price, item_desc, item_image)

iii) store_items_size (item_id, item_size)

iv) store_items_color (item_id, item_color)

 

i have inserted values into each table. My problem now is, the 1st php (seestore.php) is working, it display the list, but there is problem in my 2nd php(showitem.php),the page cant show error. i have keep checking my coding but i still cant find where is my mistakes.

Below is my php script:

 

seestore.php

<?php
// connect to database
$mysqli = mysqli_connect("xxxxx","xxxxx","xxxxx","xxxxx");

$display_block = "<h1>Product Categories</h1>
<p><b>Select a category to see its items.</b></p>";


//show categories first
$get_badminton_sql = "SELECT id, badminton_title, badminton_desc FROM
					store_categories ORDER BY badminton_title";
$get_badminton_res = mysqli_query($mysqli, $get_badminton_sql)
					or die(mysqli_error($mysqli));

if (mysqli_num_rows ($get_badminton_res) < 1) {
$display_block = "<p><em>Sorry, no categories to browse.</em></p>";
} else {
while ($badminton = mysqli_fetch_array($get_badminton_res)) {
	$badminton_id = $badminton['id'];
	$badminton_title = strtoupper (stripslashes($badminton['badminton_title']));
	$badminton_desc = stripslashes($badminton['badminton_desc']);

	$display_block .="<p><strong><a href=\"".$_SERVER["PHP_SELF"].
	"?badminton_id=".$badminton_id."\">".$badminton_title."</a></strong><br/>"
	.$badminton_desc."</p>";

	if(isset($_GET["badminton_id"])) {
		if ($_GET["badminton_id"] == $badminton_id) {
		//get items
		$get_items_sql = "SELECT id, item_title, item_price FROM
						 store_items WHERE badminton_id = '".$badminton_id."'
						 ORDER BY item_title";
		$get_items_res = mysqli_query($mysqli, $get_items_sql)
						 or die(mysqli_error($mysqli));

		if (mysqli_num_rows($get_items_res)<1) {
			$display_block = "<p><em>Sorry, no items in this
								category.</em></p>";
		} else {
			$display_block .="<ul>";
			while ($items = mysqli_fetch_array ($get_items_res)) {
				$item_id = $items['id'];
				$item_title = stripslashes($items['item_title']);
				$item_price = $items['item_price'];

				$display_block .="<li><a href=\"showitem.php?
				item_id=".$item_id."\">".$item_title."</a>
				</strong>(\$".$item_price.")</li>";
			}
			$display_block .="</ul>";
		}
		//free results
		mysqli_free_result($get_items_res);
	}
}
}
}
//free results
mysqli_free_result($get_badminton_res);
//close connection to MySQL
mysqli_close($mysqli);
?>
<html>
<head>
<title>My Categories</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>															

 

 

showitem.php

<?php  ///displaying page
//connect to database
$mysqli = mysqli_connect("xxxxx","xxxxx","xxxxx","xxxxx");

$display_block = "<h1>My Store - Item Detail</h1>";

//validate item (/////////// i don't really understand this part , and personally i think the error come from here)
$get_item_sql = "SELECT c.id as badminton_id, c.badminton_title, si.item_title,
			 si.item_price, si.item_desc, si.item_image FROM store_items 
			 AS si LEFT JOIN store_categories AS c on c.id = si.badminton_id
			 WHERE si.id = '".$_GET["item_id"]."'";
$get_item_res = mysqli_query($mysqli, $get_item_sql)
			or die(mysqli_error($mysqli));

if (mysqli_num_rows($get_item_res) <1) {
//invalid item
$display_block .="<p><em>Invalid item selection.</em></p>";
} else {
//valid item, get info
while ($item_info = mysqli_fetch_array($get_item_res)) {
	$badminton_id = $item_info['badminton_id'];
	$badminton_title = strtoupper(stripslashes($item_info['badminton_title']));
	$item_title = stripslashes($item_info['item_title']);
	$item_price = $item_info['item_price'];
	$item_desc = stripslashes($item_info['item_desc']);
	$item_image = $item_info['item_image'];
}

///make breadcrumb trail
$display_block .=<p><strong><em>You are viewing:</em><br/>
<a href=\"seestore.php?badminton_id=".$badminton_id."\">".$badminton_title."</a>
> ".$item_title."</strong></p>
<table cellpadding=\"3\" cellspacing=\"3\">
<tr>
<td valign=\"middle\" align=\"center\">
<img src=\"".$item_image."\"/></td>
<td valign=\"middle\"><p><strong>Description:</strong><br/>".
$item_desc."</p>
<p><strong>Price:</strong> \$".$item_price."</p>";

///free result
mysqli_free_result($get_item_res);

///get colors
$get_colors_sql = "SELECT item_color FROM store_items_color WHERE
					item_id = '".$GET["item_id"]."' ORDER BY item_color";
$get_colors_res = mysqli_query($mysqli, $get_colors_sql)
					or die(mysqli_error($mysqli));

if (mysqli_num_rows($get_colors_res) >0) {
	$display_block .="<p><strong>Available Colors:</strong><br/>";
	while ($colors = mysqli_fetch_array($get_colors_res)) {
		item_color = $colors['item_color'];
		$display_block .=$item_color."<br/>";
   }
}
//free result
mysqli_free_result($get_colors_res);

//get sizes
$get_sizes_sql = "SELECT item_size FROM store_items_size WHERE
				  item_id = ".$_GET["item_id"]."ORDER BY item_size";
$get_sizes_res = mysqli_query($mysqli, $get_sizes_sql)
				or die (mysqli_error($mysqli));

if (mysqli_num_rows($get_sizes_res) >0) {
	$display_block .="<p><strong>Available Sizes:</strong><br/>";
	while ($sizes = mysqli_fetch_array ($get_sizes_res)) {
		$item_size = $sizes['item_size'];
		$display_block .=$item_size."<br/>";
	}
}
//free result
mysqli_free_result($get_sizes_res);

$display_block .="
</td>
</tr>
</table>";
}
?>
<html>
<head>
<title>My Store</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>

 

Thanks for helping & sorry for bad English.

 

Link to comment
Share on other sites

Missing $ on item_color

 

while ($colors = mysqli_fetch_array($get_colors_res)) {
		item_color = $colors['item_color'];
		$display_block .=$item_color."<br/>";

 

should be:

 

while ($colors = mysqli_fetch_array($get_colors_res)) {

$item_color = $colors['item_color'];

$display_block .=$item_color."<br/>";

Link to comment
Share on other sites

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.