Jump to content

Recommended Posts

Good Evening,

 

Please see below links for a live example in test enviroment, with test data, thanks :

 

http://www.everyonlinestore.co.uk/snookereast/index.php

http://www.everyonlinestore.co.uk/snookereast/basket.php

 

i'm having real problems trying to get the errors to go away with this..

 

errors i have :

 

1) initial add to basket creates an error with the session, press the plus to add another and it goes away...

 

2) Reduce the number in basket to zero, and the session is unset, and removed from the basket, but the "your basket is empty" message doesn't appear, erroring my total line...

 

3) Empty the basket link, on the basket page, displays the "your basket is empty" message, but displays an error...

 

I've been fiddling with this all night but getting nowhere, would someone be able to offer me any advice, or point me in the right direction?

 

Thanks for all the help this forum offers!

 

Alex Howard.

 

p.s. Here's the full code i am using.

 

produts are added via - basket.php?action=add&id=$product_id

Products are removed via - basket.php?action=remove&id=$product_id

basket is emptied via - basket.php?action=empty

 

<? session_start() ?>

<style type="text/css">
<!--
.style1 {font-size: 24px}
.style2 {font-size: 12px}
.style4 {font-size: 12px; font-weight: bold; }
-->
</style>
<p><a href="http://www.everyonlinestore.co.uk/snookereast/index.php"><img src="images/logo.jpg" alt="Snooker East" border="0" longdesc="http://www.everyonlinestore.co.uk/snookereast/index.php" /></a></p>
<p class="style1">Your Shopping Basket </p>
<p class="style1"><span class='style4'><a href="basket.php?action=empty">Empty Basket</a></span></p>
<table width='800' border='1' cellspacing='0' cellpadding='0'>	
  <tr>
    <td align='left' bgcolor='#C9E4E4'><span class='style4'>Product</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Quantity</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Amount</span></td>
  </tr>

<?php

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());

 if(!isset($_GET['id'])) { $_GET['id'] = " "; } 
     $product_id = $_GET['id']; 

 if(!isset($_GET['action'])) { $_GET['action'] = " "; } 
     $action     = $_GET['action']; 

     
     function productExists($product_id) {
             
             $sql = sprintf("SELECT id FROM products "); 
                 
             return mysql_num_rows(mysql_query($sql)) > 0;
     }

     
     if($product_id && !productExists($product_id)) {
         die("Error. Product Doesn't Exist");
     }


 if($action == "add" ) {
 $_SESSION['a'][$product_id]++;
 }
 else if ($action == "remove" ) {
 $_SESSION['a'][$product_id]--;
 }
 else if($action = "empty" ) {
 unset($_SESSION['a']);
 }

 if($_SESSION['a'][$product_id] == 0 ) {
 unset($_SESSION['a'][$product_id]);
 }   

 if(isset($_SESSION['a'])) { 

foreach($_SESSION['a'] as $product_id => $quantity) {

$sql = sprintf("SELECT name, description, price, pandp FROM products WHERE id = %d;", $product_id);

$result = mysql_query($sql);

if(mysql_num_rows($result) > 0) {
                 
    list($name, $description, $price, $pandp) = mysql_fetch_row($result);

$line_cost = $price * $quantity;

if(!isset($total)) { $total = 0; }

$total = $line_cost + $total;

if(!isset($post)) { $post = 0; }

$post = $pandp;	
  
  print " 
  <tr>
    <td width='66%'><span class='style2'>$name</span></td>
    <td width='15%' align='center' valign='middle'><span class='style2'><a href='basket.php?action=remove&id=$product_id'><img src='images/minus.jpg' alt='Remove' width='15' height='15' border='0' valign='middle' /></a>
      <input type='text' value='$quantity' style='width:30' />
      <a href='basket.php?action=add&id=$product_id'><img src='images/plus.jpg' alt='Add' width='15' height='15' border='0' valign='middle' /></a></span></td>
    <td width='17%' align='right'><span class='style2'>£ $line_cost</span></td>
  </tr>";
}
}
}
else {

$total = 0.00;

  print " 
  <tr>
    <td width='66%'><span class='style2'>Your Basket is Empty...</span></td>
    <td width='15%' align='center' valign='middle'><span class='style2'>0</span></td>
    <td width='17%' align='right'><span class='style2'>0.00</span></td>
  </tr>";
  }


?>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'>Shipping & Handling Included</span></td>
  </tr>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'><strong>Total : £ <? echo $total ?></strong></span></td>
  </tr>
</table>


    <a href="index.php">Continue Shopping</a>

 

Thanks again

Link to comment
https://forums.phpfreaks.com/topic/126365-solved-error-handling/
Share on other sites

hi,

 

thanks for replying,

 

the session_start's fine, although i've changed it for good measure.  I did see it, but because it was ok i left it.  doesn't make any difference on change thou...

 

My thought's where the same, because it's trying to add to an exsisting, if it's already set. but i don't know how to fix it...

 

any ideas?

 

cheers

What is the exact error you're getting? It looks like you're not setting $_SESSION['a'][$product_id] before you increment/decrement it. That would also explain why it then works the second time.

 

If that's the case, add this before the increment/decrement code:

if (!isset($_SESSION['a'][$product_id])) $_SESSION['a'][$product_id] = 0;

excellent!!

 

isn't it obvious when you see it!

 

that's sorted out error 1 - intial adding to the basket!

 

error 3 is probably a similar affair then.

 

when you empty the basket you get the error :

 

Undefined index: a

 

presumably because it doesn't exsist anymore...

 

cheers

The root of your problem is that you are attempting to do stuff to variables that you may or may not have already set:

<?php
 if($action == "add" ) {
 $_SESSION['a'][$product_id]++;
 }
 else if ($action == "remove" ) {
 $_SESSION['a'][$product_id]--;
 }
 else if($action = "empty" ) {
 unset($_SESSION['a']);
 }

 if($_SESSION['a'][$product_id] == 0 ) {
 unset($_SESSION['a'][$product_id]);
 }   

 if(isset($_SESSION['a'])) { 

foreach($_SESSION['a'] as $product_id => $quantity) {
?>

 

Nowhere in your code to you define $_SESSION['a'] or $_SESSION['a'][$product_id]. But then you are unsetting both of those at some point. In other words, you'll get an error every time you increment, decrement, unset, or do anything else to a variable that hasn't been set or already defined.

 

You need to learn these rules yourself so you know for future projects, but try replacing the section I listed above with this:

<?php
if (!isset($_SESSION['a']) $_SESSION['a'] = array();
if (($action=="add"||$action=="remove")&&!isset($_SESSION['a'][$product_id]) $_SESSION['a'][$product_id] = 0;
 if($action == "add" ) {
 $_SESSION['a'][$product_id]++;
 }
 else if ($action == "remove" ) {
 $_SESSION['a'][$product_id]--;
 }
 else if($action = "empty" ) {
 $_SESSION['a'] = array();
 }

 if($_SESSION['a'][$product_id] == 0 ) {
 unset($_SESSION['a'][$product_id]);
 }   

foreach($_SESSION['a'] as $product_id => $quantity) {
?>

Basically what I did was define $_SESSION['a'] and keep it defined. Then, rather than check if it's defined to run your foreach, you can get rid of that, because it will just be an empty array. That will stop the foreach from doing anything. Also, you'll need to remove an extra } from the IF that I removed above the foreach.

Hi Mate,

 

cheers for all your help,

 

that's great, and with each question i ask i am learning more and more,

 

however...

 

when the basket is empty, or i press empty basket i am still getting errors...

 

i take it if it's unset, then it's set to zero,

 

however if i try to display "your basket its empty when it's zero i still get an error...

NO! Unset is different than set to zero! If a variable is unset, PHP basically thinks it doesn't exist. Being set to zero is something. Now that you've changed your code, post all of it again and I'll look at the syntax. Also, list the exact errors that you're getting, along with what lines those errors are referring to.

Thanks for all this.

 

i've learnt alot but i'm obviously still a noob!!  :D

 

i've been messing about a bit, and have commented out the section that says "your basket is empty" but here it is in it's current state.

 

thanks again!

 

<?php session_start(); ?>

<style type="text/css">
<!--
.style1 {font-size: 24px}
.style2 {font-size: 12px}
.style4 {font-size: 12px; font-weight: bold; }
-->
</style>
<p><a href="http://www.everyonlinestore.co.uk/snookereast/index.php"><img src="images/logo.jpg" alt="Snooker East" border="0" longdesc="http://www.everyonlinestore.co.uk/snookereast/index.php" /></a></p>
<p class="style1">Your Shopping Basket </p>
<p class="style1"><span class='style4'><a href="basket.php?action=empty">Empty Basket</a></span></p>
<table width='800' border='1' cellspacing='0' cellpadding='0'>	
  <tr>
    <td align='left' bgcolor='#C9E4E4'><span class='style4'>Product</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Quantity</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Amount</span></td>
  </tr>

<?php

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());

 if(!isset($_GET['id'])) { $_GET['id'] = " "; } 
     $product_id = $_GET['id']; 

 if(!isset($_GET['action'])) { $_GET['action'] = " "; } 
     $action     = $_GET['action']; 

     
     function productExists($product_id) {
             
             $sql = sprintf("SELECT id FROM products "); 
                 
             return mysql_num_rows(mysql_query($sql)) > 0;
     }

     
     if($product_id && !productExists($product_id)) {
         die("Error. Product Doesn't Exist");
     }

if (!isset($_SESSION['a'])) { $_SESSION['a'] = array(); }

if (($action=="add"||$action=="remove"||$action=="empty")&&!isset($_SESSION['a'][$product_id])) { $_SESSION['a'][$product_id] = 0; }
 if($action == "add" ) {
 $_SESSION['a'][$product_id]++;
 }
 else if ($action == "remove" ) {
 $_SESSION['a'][$product_id]--;
 }
 else if($action = "empty" ) {
 $_SESSION['a'] = array();
 }

 if($_SESSION['a'][$product_id] == 0 ) {
 unset($_SESSION['a'][$product_id]);
 }   

foreach($_SESSION['a'] as $product_id => $quantity) {

$sql = sprintf("SELECT name, description, price, pandp FROM products WHERE id = %d;", $product_id);

$result = mysql_query($sql);

if(mysql_num_rows($result) > 0) {
                 
    list($name, $description, $price, $pandp) = mysql_fetch_row($result);

$line_cost = $price * $quantity;

if(!isset($total)) { $total = 0; }

$total = $line_cost + $total;

if(!isset($post)) { $post = 0; }

$post = $pandp;	
  
  print " 
  <tr>
    <td width='66%'><span class='style2'>$name</span></td>
    <td width='15%' align='center' valign='middle'><span class='style2'><a href='basket.php?action=remove&id=$product_id'><img src='images/minus.jpg' alt='Remove' width='15' height='15' border='0' valign='middle' /></a>
      <input type='text' value='$quantity' style='width:30' />
      <a href='basket.php?action=add&id=$product_id'><img src='images/plus.jpg' alt='Add' width='15' height='15' border='0' valign='middle' /></a></span></td>
    <td width='17%' align='right'><span class='style2'>£ $line_cost</span></td>
  </tr>";
}
}

//if(($action=="empty")&&!isset($_SESSION['a'][$product_id])) {

//$total = 0.00;

//  print " 
//  <tr>
//    <td width='66%'><span class='style2'>Your Basket is Empty...</span></td>
//    <td width='15%' align='center' valign='middle'><span class='style2'>0</span></td>
//    <td width='17%' align='right'><span class='style2'>0.00</span></td>
//  </tr>";
//  }


?>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'>Shipping & Handling Included</span></td>
  </tr>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'><strong>Total : £ <? echo $total ?></strong></span></td>
  </tr>
</table>


    <a href="index.php">Continue Shopping</a>

OK, I made some notes and changes. Use this code, then tell me the EXACT error you're getting with the line number:

<?php session_start(); ?>

<style type="text/css">
<!--
.style1 {font-size: 24px}
.style2 {font-size: 12px}
.style4 {font-size: 12px; font-weight: bold; }
-->
</style>
<p><a href="http://www.everyonlinestore.co.uk/snookereast/index.php"><img src="images/logo.jpg" alt="Snooker East" border="0" longdesc="http://www.everyonlinestore.co.uk/snookereast/index.php" /></a></p>
<p class="style1">Your Shopping Basket </p>
<p class="style1"><span class='style4'><a href="basket.php?action=empty">Empty Basket</a></span></p>
<table width='800' border='1' cellspacing='0' cellpadding='0'>	
  <tr>
    <td align='left' bgcolor='#C9E4E4'><span class='style4'>Product</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Quantity</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Amount</span></td>
  </tr>

<?php
include ("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass) or die("Could not connect to mysql because " . mysql_error());

//select the database	
mysql_select_db($db) or die("Could not select database because " . mysql_error());

// I replaced your two if statements with these:
$product_id = isset ($_GET['id']) ? $_GET['id'] : " ";
$action = isset ($_GET['action']) ? $_GET['action'] : " ";

function productExists($product_id) {
$sql = sprintf("SELECT id FROM products "); // You're missing stuff in your query here, specifically a WHERE clause
return mysql_num_rows(mysql_query($sql)) > 0;
}

if (!productExists($product_id)) {
die("Error. Product Doesn't Exist");
}

if (!isset ($_SESSION['a'])) {
$_SESSION['a'] = array ();
}

if (($action == "add" || $action == "remove") && !isset ($_SESSION['a'][$product_id])) {
$_SESSION['a'][$product_id] = 0;
}

if ($action == "add") {
$_SESSION['a'][$product_id]++;
}
elseif ($action == "remove") {
$_SESSION['a'][$product_id]--;
}
elseif ($action = "empty") {
$_SESSION['a'] = array ();
}

if ($_SESSION['a'][$product_id] == 0) {
unset ($_SESSION['a'][$product_id]);
}

foreach ($_SESSION['a'] as $product_id => $quantity) {

$sql = sprintf("SELECT name, description, price, pandp FROM products WHERE id = %d;", $product_id);

$result = mysql_query($sql);

if (mysql_num_rows($result) > 0) {

	list ($name, $description, $price, $pandp) = mysql_fetch_row($result);

	$line_cost = $price * $quantity;

	if (!isset ($total)) {
		$total = 0;
	}

	$total = $line_cost + $total;

	if (!isset ($post)) {
		$post = 0;
	}

	$post = $pandp;

	print " 
			  <tr>
			    <td width='66%'><span class='style2'>$name</span></td>
			    <td width='15%' align='center' valign='middle'><span class='style2'><a href='basket.php?action=remove&id=$product_id'><img src='images/minus.jpg' alt='Remove' width='15' height='15' border='0' valign='middle' /></a>
			      <input type='text' value='$quantity' style='width:30' />
			      <a href='basket.php?action=add&id=$product_id'><img src='images/plus.jpg' alt='Add' width='15' height='15' border='0' valign='middle' /></a></span></td>
			    <td width='17%' align='right'><span class='style2'>£ $line_cost</span></td>
			  </tr>";
}
}

//if(($action=="empty")&&!isset($_SESSION['a'][$product_id])) {

//$total = 0.00;

//  print " 
//  <tr>
//    <td width='66%'><span class='style2'>Your Basket is Empty...</span></td>
//    <td width='15%' align='center' valign='middle'><span class='style2'>0</span></td>
//    <td width='17%' align='right'><span class='style2'>0.00</span></td>
//  </tr>";
//  }
?>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'>Shipping & Handling Included</span></td>
  </tr>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'><strong>Total : £ <? echo $total ?></strong></span></td>
  </tr>
</table>


    <a href="index.php">Continue Shopping</a>

Hi,

 

Apologies, i meant to write the error!

 

It's when you press "Empty Basket"

 

Undefined index: in E:\********************\basket.php on line 60

 

What i'd like to happen is either when you reduce the quantity to zero, and it unsets the session, (so nothing is in the basket) you get "your basket is empty", or when you press "Empty basket"

 

i only get this error at the moment when you press empty basket, not by reducing the quantity

 

Thanks again for your help

 

p.s. this is using the code above (your new one)

 

 

Excellent!

 

Thankyou so much, it's working! error free! ;D

 

i've used the code below to show the basket empty message, and all's cool!

 

if (!isset($_SESSION['a'][$product_id])&&@$_SESSION['a'][$product_id] == 0) {

 

Thanks again!!

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.