Jump to content
Larry Ullman's Book Forums

Remove From Wishlist Link Not Working


Recommended Posts

Hello All.

 

The link "Remove from wishlist", on the Wishlist page is not working. i.e, it's not removing item from the Wishlist.

 

It could be the wishlist.html or the wishlist.php (I'm not sure). Here's the code for both. Thank you in advance!!

 

wishlist.html (php part)

<?php

// Fetch each item:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
	$price = get_just_price($row['price'], $row['sale_price']);
	$subtotal = $price * $row['quantity'];
	echo '<tr>
		<td>' . $row['category'] . '::' . $row['name'] . '</td>
		<td align="center"><input type="text" name="quantity[' . $row['sku'] . ']" value="' . $row['quantity'] . '" size="2" class="small" /></td>
		<td align="right">$' . number_format($price, 2) . '</td>
		<td align="right">$' . number_format($subtotal, 2) . '</td>
		<td align="right"><a href="/cart.php?sku=' . $row['sku'] . '&action=move&qty=' . $row['quantity'] .'">Move to Cart</a><br /><a href="/wishlist.php?sku=' . $row['sku'] . '&action=remove">Remove from Wish List</a></td>
	</tr>
	';
	
	// Check the stock status:
	if ( ($row['stock'] > 0) && ($row['stock'] < 10)) {
		echo '<tr class="error"><td colspan="5" align="center">There are only ' . $row['stock'] . ' left in stock of the ' . $row['name'] . '.</td></tr>';
	}

} // End of WHILE loop. 

?>         </table><p align="center"><input type="submit" value="Update Quantities" class="button" /></form></p></div>	

 

 

wishlist.php

 

// If there's a SKU value in the URL, break it down into its parts:
if (isset($_GET['sku'])) {
	list($sp_type, $pid) = parse_sku($_GET['sku']);
}

if (isset ($sp_type, $pid, $_GET['action']) && ($_GET['action'] == 'remove') ) { // Remove it from the wish list.
	
	$r = mysqli_query($dbc, "CALL remove_from_wish_list('$uid', '$sp_type', $pid)");

} elseif (isset ($sp_type, $pid, $_GET['action'], $_GET['qty']) && ($_GET['action'] == 'move') ) { // Move it to the wish list.

	// Determine the quantity:
	$qty = (filter_var($_GET['qty'], FILTER_VALIDATE_INT, array('min_range' => 1))) ? $_GET['qty'] : 1;

	// Add it to the wish list:
$r = mysqli_query($dbc, "SELECT id FROM carts where user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");
if (mysqli_num_rows($r) == 1) { // Exists in cart, UPDATE!
    list($cid) = mysqli_fetch_array($r, MYSQLI_NUM);
    $r = mysqli_query($dbc, "UPDATE carts SET quantity=quantity+1, date_modified=NOW() WHERE id=$cid");
} else { // Not in cart, INSERT!
    $r = mysqli_query($dbc, "INSERT INTO carts (user_session_id, product_type, product_id, quantity) VALUES ('$uid', '$sp_type', $pid, 1)");
}

	
	// Remove it from the cart:
	$r = mysqli_query($dbc, "DELETE FROM carts WHERE user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");

	
} elseif (isset($_POST['quantity'])) { // Update quantities in the wish list.
	
	// Loop through each item:
	foreach ($_POST['quantity'] as $sku => $qty) {
		
		// Parse the SKU:
		list($sp_type, $pid) = parse_sku($sku);
		
		if (isset($sp_type, $pid)) {

			// Determine the quantity:
			$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;

			// Update the quantity in the wish list:

if ($qty > 0) {
    $r = mysqli_query($dbc, "UPDATE carts SET quantity=$qty, date_modified=NOW() WHERE user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");
} elseif ($qty == 0) {
    $r = mysqli_query($dbc, "DELETE FROM carts WHERE user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");
}

		} // End of FOREACH loop.
	
	}// End of main IF.
}		
// Get the wish list contents:
$r = mysqli_query($dbc, "SELECT CONCAT('O', ncp.id) AS sku, c.quantity, ncc.category, ncp.name, ncp.price, ncp.stock, sales.price AS sale_price
						FROM carts AS c INNER JOIN non_coffee_products AS ncp ON c.product_id=ncp.id
						INNER JOIN non_coffee_categories AS ncc ON ncc.id=ncp.non_coffee_category_id
						LEFT OUTER JOIN sales ON (sales.product_id=ncp.id AND sales.product_type='other'
						AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) )
						WHERE c.product_type='other' AND c.user_session_id='$uid'
						UNION SELECT CONCAT('C', sc.id), c.quantity, gc.category, CONCAT_WS(' - ', s.size, sc.caf_decaf, sc.ground_whole), sc.price, sc.stock, sales.price
						FROM carts AS c INNER JOIN specific_coffees AS sc ON c.product_id=sc.id INNER JOIN sizes AS s ON s.id=sc.size_id
						INNER JOIN general_coffees AS gc ON gc.id=sc.general_coffee_id LEFT OUTER JOIN sales ON (sales.product_id=sc.id
						AND sales.product_type='coffee' AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) )
						WHERE c.product_type='coffee' AND c.user_session_id='$uid'");


if (mysqli_num_rows($r) > 0) { // Products to show!
	include ('./views/wishlist.html');
} else { // Empty cart!
	include ('./views/emptylist.html');
}

// Finish
include ('./includes/footer.html');
?>

 

 

 

Link to comment
Share on other sites

.... I have put the inserted code from your blog (http://www.larryullm...l-3-chapter-10/) to replace stored procedures, and put the 'Session ID' through SQL on PHPMYADMIN and have come up with the following error on line one.
 
 
Error

SQL query: b_help.png

"SELECT CONCAT('O', ncp.id) AS sku, c.quantity, ncc.category, ncp.name, ncp.price, ncp.stock, sales.price AS sale_price FROM carts AS c INNER JOIN non_coffee_products AS ncp ON c.product_id=ncp.id INNER JOIN non_coffee_categories AS ncc ON ncc.id=ncp.non_coffee_category_id LEFT OUTER JOIN sales ON (sales.product_id=ncp.id AND sales.product_type='other' AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) ) WHERE c.product_type='other' AND c.user_session_id='$uid' UNION SELECT CONCAT('C', sc.id), c.quantity, gc.category, CONCAT_WS(' - ', s.size, sc.caf_decaf, sc.ground_whole), sc.price, sc.stock, sales.price FROM carts AS c INNER JOIN specific_coffees AS sc ON c.product_id=sc.id INNER JOIN sizes AS s ON s.id=sc.size_id INNER JOIN general_coffees AS gc ON gc.id=sc.general_coffee_id LEFT OUTER JOIN sales ON (sales.product_id=sc.id AND sales.product_type='coffee' AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) ) WHERE c.product_type='coffee' AND c.user_session_id='$uid'"

MySQL said: b_help.png

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SELECT CONCAT('O', ncp.id) AS sku, c.quantity, ncc.category, ncp.name, ncp.pric' at line 1 
Link to comment
Share on other sites

Happy Easter Larry.

 

I am still encountering problems with line 1 after trying various combinations, such as

  • removing the quotation wraps
  • changing the quotation to single quotes
  • removing or changing the quotation of the variables, so they don't clash with the opening quotes

... still no joy.

 

Could it be something to do with this section of the code?

 

	// Remove it from the wish list:
	$r = mysqli_query($dbc, "CALL remove_from_wish_list('$uid', '$sp_type', $pid)");

} elseif (isset($_POST['quantity'])) { // Update quantities in the cart.
	
	// Loop through each item:
	foreach ($_POST['quantity'] as $sku => $qty) {
		
		// Parse the SKU:
		list($sp_type, $pid) = parse_sku($sku);
		
		if (isset($sp_type, $pid)) {

			// Determine the quantity:
			$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;

			// Update the quantity in the cart:
			if ($qty > 0) {
				$r = mysqli_query($dbc, "UPDATE carts SET quantity=$qty, date_modified=NOW() WHERE user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");
			} elseif ($qty == 0) {
				$r = mysqli_query($dbc, "DELETE FROM carts WHERE user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");
			}
		}
			
	} // End of FOREACH loop.
	
}// End of main IF.

 

 

My testing site is: www.lickmyartlondon.co.uk  

 

It's a 'Logical Error' so I'm not getting any error messages showing on the site.......

 

Thank you kindly Sir

Link to comment
Share on other sites

Happy Easter to you as well. The code you just posted uses one stored procedure, which you say you're getting rid of. The SQL command you posted previously, you had apparently taken out of the code directly and executed in phpMyAdmin. That's where the problematic quotes came from. Of course, even without those quotes, the query wouldn't have run properly because you don't have the value of $uid in there. But that kind of doesn't matter anyway, because that query fetches the contents of the wish list and the problem is with the query that should be updating the wishlist. 

 

I'd start by confirming that:

A) The removal query is being executed

B ) What the query's results or response is

Link to comment
Share on other sites

Hail L-U!!

 

I can confirm that

 

cart.php

  • "Move to wishlist" link works fine
  • "Remove from cart" link doesn't works at all

wishlist.php

 

  • "Move to cart" link works fine
  • "Remove from wishlist" link doesn't works at all

 

Is this what you wanted me to confirm?

Link to comment
Share on other sites

That's a start. Now you should confirm if the appropriate section of code (where the removal query is) is being executed at all. Then you should confirm the results of the queries in those sections of code. This is all basic debugging stuff.

Link to comment
Share on other sites

... I know what this looks like Larry, it's turning into a debugging lesson (lol)

 

to debug I've done this to debug that line of code.


<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

echo '<pre>';
print_r(

// For debugging purposes:
if (!$r) echo mysqli_error($dbc);

} elseif (isset ($sp_type, $pid, $_GET['action']) && ($_GET['action'] == 'remove') ) { // Remove it from the cart.

$r = mysqli_query($dbc, "DELETE FROM carts WHERE user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");

} elseif (isset ($sp_type, $pid, $_GET['action'], $_GET['qty']) && ($_GET['action'] == 'move') ) { // Move it to the cart.

// Determine the quantity:
$qty = (filter_var($_GET['qty'], FILTER_VALIDATE_INT, array('min_range' => 1))) ? $_GET['qty'] : 1;

// Add it to the cart:
$r = mysqli_query($dbc, "SELECT id FROM carts where user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");

if (mysqli_num_rows($r) == $qty) { // Exists in cart, UPDATE!

    list($cid) = mysqli_fetch_array($r, MYSQLI_NUM);
    $r = mysqli_query($dbc, "UPDATE carts SET quantity=quantity+$qty, date_modified=NOW() WHERE id=$cid");
} else { // Not in cart, INSERT!
    $r = mysqli_query($dbc, "INSERT INTO carts (user_session_id, product_type, product_id, quantity) VALUES ('$uid', '$sp_type', $pid, $qty)");
}


// Remove it from the wish list:
$r = mysqli_query($dbc, "CALL remove_from_wish_list('$uid', '$sp_type', $pid)");

} elseif (isset($_POST['quantity'])) { // Update quantities in the cart.

)
echo '</pre>';

 

 

 with the following error:

 

Parse error: syntax error, unexpected T_IF, expecting ')' in /hermes/waloraweb004/b1384/moo.laissezfairelondonco/lickmyartlondon/cart.php on line 50

 

Any good?

Link to comment
Share on other sites

No, parse errors are not good. I see at least two, if not three, just giving it a cursory glance. Do you not understand what I mean by " Now you should confirm if the appropriate section of code (where the removal query is) is being executed at all."?

Link to comment
Share on other sites

 Share

×
×
  • Create New...