Jump to content
Larry Ullman's Book Forums

armlocker

Members
  • Content Count

    83
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by armlocker


  1. Hello again Yoda of PHP

     

    I have echoed the $sql; under the select query above and now I get the :

     

    Notice: Undefined variable: sql in C:\xampp\htdocs\game\index.php on line 16

     

    my database (questions) structure is simple. here are the coloumns

     

    questions_record_number

    question_id

    question

    answer

    option1

    option2

    option3

    option4

    subject

     

    NOTHING IS WORKING AT THE MOMENT - AND HERES WHATS SHOWING

     

    ===============================================================

    QUESTION 1

    How many bytes are used to represent the primitive...? 

    Choose the right answer:

    Notice: Undefined variable: sql in C:\xampp\htdocs\game\index.php on line 16
    2
    4
    8
    1
    correct answer

    ==============================================================

     

    I know its something really simple - I just cant figure it out, and is an excellent exercise for getting results from the database, useful in many situations.

     

    .... over to you Sir

     


  2. Hi there, just trying to get some practice in by creating a simple php game - can someone tell me where am I going wrong please: Thanks in advance

    <?php
    $con = mysql_connect('localhost', 'root', '');
    mysql_select_db("krish");
    ?>
    
    QUESTION 1<br><br>
    
    How many bytes are used to represent the primitive...? <br><br>
        
        
    Choose the right answer:<br><br>
    
    <?php
    
    $query = mysql_query("SELECT * FROM questions WHERE question_id = 'Q1'");
    
    while ($row = mysql_fetch_assoc($query)) {
        echo $row['option1'] . '<input type="radio" name="option_1" value="option1"><br>';   
        echo $row['option2'] . '<input type="radio" name="option_1" value="option2"><br>';  
        echo $row['option3'] . '<input type="radio" name="option_1" value="option3"><br>';  
        echo $row['option4'] . '<input type="radio" name="option_1" value="option4"><br>';      
    }
    
    $var = "select from answer questions where answer ='4'";
    
    if (isset($var)) {
        echo 'correct answer';
    } else {
        echo 'wrong answer';
    }
    ?>
    
    

  3. .... you see these parts in the script; they are meant to bring the user back to checkout.php after they press the pay button.

    <br /><br />'.$checkout_button.'</p>';
    
    

    and this part:

    <input type="hidden" name="notify_url" Value="http://www.wolfcut.co.uk/checkout.php" />
    
    

    I was trying this; to link the checkout button;  which didn't work either:

    <br /><br /><a href="checkout.php"'.$checkout_button.'</a></p>';
    
    

  4. Hi HartleySan

     

    Yep, I've just spotted that typo too, should be:

    session_id($cid);
    

    the view_cart.php with the added paypal button looks like this - which is not linking to the checkout.php after the customer presses the buy button:

    		echo "\t<tr>
    		<td align=\"left\">{$row['artist']}</td>
    		<td align=\"left\">{$row['print_name']}</td>
    		<td align=\"right\">£{$_SESSION['cart'][$row['print_id']]['price']}</td>
    		<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]['quantity']}\" /></td>
    		<td align=\"right\">£" . number_format ($subtotal, 2) . "</td>
    		</tr>\n";
    		 $string_of_item_names = $string_of_item_names.' <input type="hidden" name="item_name_'.$count.'" Value="'.$row['print_name'].'" >'; # build item name string of all items in the basket
      $string_of_item_amounts = $string_of_item_amounts.' <input type="hidden" name="amount_'.$count.'" Value="'. number_format ($subtotal, 2).'" >'; # build string of prices for items in basket
      # use count as a reference to match names with prices in paypal
      $count++;
    	
    	} // End of the WHILE loop.
    
    	mysqli_close($con); // Close the database connection.
    
    if ($_SESSION['userid']>0)	$checkout_button = ('<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    
        <!-- Identify your business so that you can collect the payments.
        HELP: https://www.paypal.com/us/cgi-bin/webscr?cmd=_pdn_xclick_techview_outside
        https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/#id08A6HF00TZS
        
        We use cart becuase we have our own (third party which is us)
         -->
        <input type="hidden" name="business" value="hello@richmond">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        
        
        <!--- this is where the user comes back to after they finished making the payment --->
        <input type="hidden" name="notify_url" Value="http://www.wolfcut.co.uk/checkout.php" />
    
        <!-- Specify details about the item that buyers will purchase. -->
        '.$string_of_item_names.'
    	'.$string_of_item_amounts.'
        <input type="hidden" name="currency_code" value="GBP">
    
    
        <!-- Display the payment button. -->
        <div align="center">
    	<input type="image" name="submit" border="0" align="center" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
            alt="PayPal - The safer, easier way to pay online">
        <img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
    	</div>
    </form>');
    else 
    	$checkout_button = '<a href="login.php">You must be logged in to purchase or please sign up.</a>';
    	// Print the total, close the table, and the form:
    	echo '<tr>
    		<td colspan="4" align="right"><b>Total:      </b></td>
    		<td align="right">£' . number_format ($total, 2) . '</td>
    	</tr>
    	</table>
    	<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
    	</form><p align="center">Enter a quantity of 0 to remove an item.
    	<br /><br />'.$checkout_button.'</p>';
    
    

  5. Hi All.

     

    I can't get a redirect back to the "checkout.php" in chapter 19.

     

    I am using a personalised paypal button.

     

    Firstly; are these lines correct in checkout.php?

    // customer is logged in and this page has access to the customer's ID:
    session_id)£id);
    
    // this page receives the order total: $subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];
    

    Secondly; heres the code from view_cart.php with my paypal button, but the problem is that it's not directing it back to checkout.php (above code may be incorrect?)

        <!--- this is where the user comes back to after they finished making the payment --->
        <input type="hidden" name="notify_url" Value="http://wolfcut.co.uk/checkout.php" />
    
        <!-- Specify details about the item that buyers will purchase. -->
        '.$string_of_item_names.'
    	'.$string_of_item_amounts.'
        <input type="hidden" name="currency_code" value="GBP">
    
    
        <!-- Display the payment button. -->
        <div align="center">
    	<input type="image" name="submit" border="0" align="center" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
            alt="PayPal - The safer, easier way to pay online">
        <img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
    	</div>
    
    

    Any advice is appreciated. Thanking you!


  6. Hi All

     

    How do I add shipping_costs to view_cart.php. It's getting more complicated the more I think about real life situations because shipping costs is determined by weight.

     

    The column 'shipping_cost' already added to database, so do I plus (+) the shipping cost to quantity, or times (x) shipping cost by quantity? Can someone show me both ways as it probably depends on the situation in real life.

     

    Here's the view_cart.php section that needs the extra code:

    		// Calculate the total and sub-totals.
    		$subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];
    		
    		$total += $subtotal;
    		
    		// Print the row:
    		echo "\t<tr>
    		<td align=\"left\">{$row['artist']}</td>
    		<td align=\"left\">{$row['print_name']}</td>
    		<td align=\"right\">£{$_SESSION['cart'][$row['print_id']]['price']}</td>
    		<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]['quantity']['shipping_cost']}\" /></td>
    		<td align=\"right\">£" . number_format ($subtotal, 2) . "</td>
    		</tr>\n";
     

  7. Thank guys. I managed to figure this out: here's what I ended up doing which worked:

    		echo (is_null($row['size'])) ? '(No size information available)' : $row['size'];
    		echo "<br />£{$row['price']} ";
    		if($row['in_stock']=='no')		
    			echo "<br>Out of stock. Email us to let us know and we will do everything we can to get it for you.";	
    		else 
    			echo "<a href=\"add_cart.php?pid=$pid\">Add to Cart</a>";
    		echo "<br >";	
    		echo "Items in stock: ".$row['stock_available'];	
    		echo "</div><br />";
    
    

  8. Hello

     

    I keep getting "Your cart is currently empty." regardless of what is added to the cart.

     

    Here are two scripts: add_cart.php and view_cart.php

     

    view_cart.php

    <?php # Script 19.10 - view_cart.php
    // This page displays the contents of the shopping cart.
    // This page also lets the user update the contents of the cart.
    
    // Set the page title and include the HTML header:
    $page_title = 'View Your Shopping Cart';
    include ('header.html');
    
    // Check if the form has been submitted (to update the cart):
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    	// Change any quantities:
    	foreach ($_POST['qty'] as $k => $v) {
    
    		// Must be integers!
    		$pid = (int) $k;
    		$qty = (int) $v;
    		
    		if ( $qty == 0 ) { // Delete.
    			unset ($_SESSION['cart'][$pid]);
    		} elseif ( $qty > 0 ) { // Change quantity.
    			$_SESSION['cart'][$pid]['quantity'] = $qty;
    		}
    		
    	} // End of FOREACH.
    	
    } // End of SUBMITTED IF.
    
    // Display the cart if it's not empty...
    if (!empty($_SESSION['cart'])) {
    
    	// Retrieve all of the information for the prints in the cart:
    	include_once('common.php'); // Connect to the database.
    	$q = "SELECT print_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name FROM artists, prints WHERE artists.artist_id = prints.artist_id AND prints.print_id IN (";
    	foreach ($_SESSION['cart'] as $pid => $value) {
    		$q .= $pid . ',';
    	}
    	$q = substr($q, 0, -1) . ') ORDER BY artists.last_name ASC';
    	$r = mysqli_query ($con, $q);
    	
    	// Create a form and a table:
    	echo '<form action="view_cart.php" method="post">
    	<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
    	<tr>
    		<td align="left" width="30%"><b>Artist</b></td>
    		<td align="left" width="30%"><b>Print Name</b></td>
    		<td align="right" width="10%"><b>Price</b></td>
    		<td align="center" width="10%"><b>Qty</b></td>
    		<td align="right" width="10%"><b>Total Price</b></td>
    	</tr>
    	';
    
    	// Print each item...
    	$total = 0; // Total cost of the order.
    	$count=1;
    	while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
    	
    		// Calculate the total and sub-totals.
    		$subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];
    		$total += $subtotal;
    		
    		// Print the row:
    		echo "\t<tr>
    		<td align=\"left\">{$row['artist']}</td>
    		<td align=\"left\">{$row['print_name']}</td>
    		<td align=\"right\">£{$_SESSION['cart'][$row['print_id']]['price']}</td>
    		<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]['quantity']}\" /></td>
    		<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
    		</tr>\n";
    		 $string_of_item_names = $string_of_item_names.' <input type="hidden" name="item_name_'.$count.'" Value="'.$row['print_name'].'" >'; # build item name string of all items in the basket
      $string_of_item_amounts = $string_of_item_amounts.' <input type="hidden" name="amount_'.$count.'" Value="'. number_format ($subtotal, 2).'" >'; # build string of prices for items in basket
      # use count as a reference to match names with prices in paypal
      $count++;
    	
    	} // End of the WHILE loop.
    
    	mysqli_close($con); // Close the database connection.
    
    	$checkout_button = ('<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    
        <!-- Identify your business so that you can collect the payments.
        HELP: https://www.paypal.com/us/cgi-bin/webscr?cmd=_pdn_xclick_techview_outside
        https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/#id08A6HF00TZS
        
        We use cart becuase we have our own (third party which is us)
         -->
        <input type="hidden" name="business" value="mystore@gmail.com">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        
        
        <!--- this is where the user comes back to after they finished making the payment --->
        <input type="hidden" name="notify_url" Value="http://www.ecom/thankyou.php" />
    
        <!-- Specify details about the item that buyers will purchase. -->
        '.$string_of_item_names.'
    	'.$string_of_item_amounts.'
        <input type="hidden" name="currency_code" value="GBP">
    
    
        <!-- Display the payment button. -->
        <input type="image" name="submit" border="0"
            src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
            alt="PayPal - The safer, easier way to pay online">
        <img alt="" border="0" width="1" height="1"
            src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
    </form>');
    	// Print the total, close the table, and the form:
    	echo '<tr>
    		<td colspan="4" align="right"><b>Total:</b></td>
    		<td align="right">$' . number_format ($total, 2) . '</td>
    	</tr>
    	</table>
    	<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
    	</form><p align="center">Enter a quantity of 0 to remove an item.
    	<br /><br />'.$checkout_button.'</p>';
    
    } else {
    	echo '<p>Your cart is currently empty.</p>';
    }
    
    include ('footer.html');
    ?>
    

    add_cart.php

    <?php # Script 19.9 - add_cart.php
    // This page adds prints to the shopping cart.
    
    // Set the page title and include the HTML header:
    $page_title = 'Add to Cart';
    include ('header.html');
    
    if (isset ($_GET['pid']) && filter_var($_GET['pid'], FILTER_VALIDATE_INT, array('min_range' => 1))  ) { // Check for a print ID.
    	$pid = $_GET['pid'];
    
    	// Check if the cart already contains one of these prints;
    	// If so, increment the quantity:
    	if (isset($_SESSION['cart'][$pid])) {
    
    		$_SESSION['cart'][$pid]['quantity']++; // Add another.
    
    		// Display a message:
    		echo '<p>Another copy of the print has been added to your shopping cart.</p>';
    		
    	} else { // New product to the cart.
    
    		// Get the print's price from the database:
    		include_once('common.php'); // Connect to the database.
    		$q = "SELECT price FROM prints WHERE print_id=$pid";
    		$r = mysqli_query ($con, $q);		
    		if (mysqli_num_rows($r) == 1) { // Valid print ID.
    	
    			// Fetch the information.
    			list($price) = mysqli_fetch_array ($r, MYSQLI_NUM);
    			
    			// Add to the cart:
    			$_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price);
    
    			// Display a message:
    			echo '<p>The print has been added to your shopping cart.</p>';
    
    		} else { // Not a valid print ID.
    			echo '<div align="center">This page has been accessed in error!</div>';
    		}
    		
    		mysqli_close($con);
    
    	} // End of isset($_SESSION['cart'][$pid] conditional.
    
    } else { // No print ID.
    	echo '<div align="center">This page has been accessed in error!</div>';
    }
    
    include ('footer.html');
    ?>
    

    Thanks


  9. ... 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?


  10. 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


  11. .... 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 

  12. 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');
    ?>
    

     

     

     


  13. Thanks Larry.

    By the way, the PHP and MySQL for Dynamic Web Sites 4th edition was a really good guide for beginners. I went through the exercises and found it really easy set up the art ecommerce in the last chapter. For my future reference, how did you become so good at coding?

    I hand copied many chapters of the code from the PHP book and found that really useful in understanding learned a hell of lot, but I sure as hell couldn't write my own code or even know how to structure a simple script. (maybe I'm not trying hard enough).

    So, what tips exercises can you give me?


  14. Hi Larry - good to speaking to you again.

     

    Yes, sorry, what I meant was an error message like this. I think I will purchase the SSL because I want it to be as realistic as possible

     

     

    This webpage is not available

    Google Chrome could not load the webpage because http took too long to respond. The website may be down, or you may be experiencing issues with your Internet connection.
    Here are some suggestions:
    • Reload this webpage later.
    • Check your Internet connection. Restart any router, modem, or other network devices you may be using.
    • Add Google Chrome as a permitted program in your firewall's or antivirus software's settings. If it is already a permitted program, try deleting it from the list of permitted programs and adding it again.
    • If you use a proxy server, check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server, adjust your proxy settings: Go to the Chrome menu >Settings > Show advanced settings... > Change proxy settings... > LAN Settings and deselect the "Use a proxy server for your LAN" checkbox.

  15. Hi guys.  Finally at the end of Chapter 10.
     
    I have a problem with the wishlist.php.
    The cart.php works fine, and they are nearly the same.
     
    When I transfer stuff to the 'Wishlist', I get this error:
     
     

    Parse error: syntax error, unexpected $end in /hermes/waloraweb004/b1384/moo.laissezfairelondonco/wolfcut.co.uk/wishlist.php on line 110
     

    I have been trying to locate the missing parenthesis error from beginning to end for half a day, so I guess its time to ask for help. I've made sure all the single and double quotations are as they should be, so I don't think its that.
     

    The script is from downloads. Only the pathways and stored procedures sections are different.

    <?php
    
    // This file manages the wish list.
    // This script is begun in Chapter 9.
    
    // Require the configuration before any PHP code:
    require ('./includes/config.inc.php');
    
    // Check for, or create, a user session:
    if (isset($_COOKIE['SESSION'])) {
    	$uid = $_COOKIE['SESSION'];
    } else {
    	$uid = md5(uniqid('biped',true));
    }
    
    // Send the cookie:
    setcookie('SESSION', $uid, time()+(60*60*24*30));
    
    // Include the header file:
    $page_title = 'Coffee - Your Wish List';
    include ('./includes/header.html');
    
    // Require the database connection:
    require ('../mysql.inc.php');
    
    // Need the utility functions:
    include ('./includes/product_functions.inc.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)");
    }
    
    	
    	// For debugging purposes:
    	if (!$r) echo mysqli_error($dbc);
    	
    	// 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");
    
    	
    	// For debugging purposes:
    	if (!$r) echo mysqli_error($dbc);
    
    } 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 the page:
    include ('./includes/footer.html');
    ?>
    
×
×
  • Create New...