Jump to content

Recommended Posts

	// does the product exist ?
$sql = "SELECT pd_id, pd_qty
        FROM tbl_product
		WHERE pd_id = $productId";
$result = dbQuery($sql);

if (dbNumRows($result) != 1) {
	// the product doesn't exist
	header('Location: cart.php');
} else {
	// how many of this product we
	// have in stock
	$row = dbFetchAssoc($result);
	$currentStock = $row['pd_qty'];

	if ($currentStock == 0) {
		// we no longer have this product in stock
		// show the error message
		setError('The product you requested is no longer in stock');
		header('Location: cart.php');
		exit;
	}

}		

// current session id
$sid = session_id();
session_register("size");
$size = $_SESSION['size'];
// check if the product is already
// in cart table for this session
$sql = "SELECT pd_id
        FROM tbl_cart
		WHERE pd_id = $productId AND ct_session_id = '$sid'";
$result = dbQuery($sql);

if (dbNumRows($result) == 0) {
	// put the product in cart table
	$sql = "INSERT INTO tbl_cart (pd_id, ct_qty, size, ct_session_id, ct_date) " .
			"VALUES ($productId, 1, '$size', '$sid', NOW())";
	$result = dbQuery($sql);
} else {
	// update product quantity in cart table
	$sql = "UPDATE tbl_cart 
	        SET ct_qty = ct_qty + 1
			WHERE ct_session_id = '$sid' AND pd_id = $productId";		

	$result = dbQuery($sql);		
}	

 

 

This is just a piece of my cartfunctions.php (which is an include() once you add an item to the cart).

I've been trouble shooting this all day and finally got it to stop giving me error messages. Im trying to allow the user to pick a size between 4 options, after that it is added to the cart and I can easily find out what size shirt the customer wants before I ship it. but now it wont update the $size variable to the DB. Am I defining this wrong or maybe my whole approach is messed up? If you would like to see the setup you can go to rbcrime.com/newshop/newshop/ .

 

 

Link to comment
https://forums.phpfreaks.com/topic/212708-problem-defining-session/
Share on other sites

That was the functions that are called upon after you add to cart but maybe the problem lies in my productdetail.php? would give the answer. I still cannot figure this out and its driving me crazzzzyyyyy. lol. if anyone can help I would really appreciate it, thanks!

 

<?php
if (!defined('WEB_ROOT')) {
exit;
}

$product = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url
extract($product);
?> 
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr> 
  <td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>"></td>
  <td valign="middle">
<strong><?php echo $pd_name; ?></strong><br>
Price : <?php echo displayAmount($pd_price); ?><br>
<?php
// if we still have this product in stock
// show the 'Add to cart' button
if ($pd_qty > 0) {
?>
<br>
<br>
Size: 
<select size="1" name="size">
  <option value="Small">Small</option>
  <option>Medium</option>
  <option>Large</option>
  <option>Extra Large</option>
  </select>
<br>
<input type="button" name="btnAddToCart" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton">

<?php
} else {
echo 'Out Of Stock';
}
?>
  </td>
</tr>
<tr align="left"> 
  <td colspan="2"><?php echo $pd_description; ?></td>
</tr>
</table>

One issue I see is in your form you're not setting any values for the sizes field

<select size="1" name="size">
  <option value="Small">Small</option>
  <option>Medium</option>
  <option>Large</option>
  <option>Extra Large</option>
  </select>

You're deifing the labels but no values! For each option (<option></option>) you need to set a value. You've only set a value for the first option.

 

To get the selected value you'd use either $_POST['size'] or $_GET['size'] (depending on your forms submit method).

 

Also when setting session variables do not use the session_register function.

	session_register("size");
$size = $_SESSION['size'];

To set a session variable you'd use $_SESSION['session_variable_name'] = 'some value';

To set thr size session you'd use

$_SESSION['size'] = $size;

 

Just remember to call session_start at the top of all pages that uses sessions.

If your forms submit method is POST, then you'd get the selected size from the $_POST['size'] variable

$size = $_POST['size'];

 

To assign the chosen size to the session you'd do

$_SESSION['size'] = $size;

 

To get the size from the session you'd use the $_SESSION['size'] variable.

Can anyone please help? I just need a simple dropdown box with 4 choices (IE: Small, Med, Large, XL) that can be defined as a variable to be added in a DB. It seems easy but it is kicking my butt. It is the ww.phpwebcommerce.com cart if it makes it easier. Any help would be greatly appreciated, thank you!

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.