Problem with variable declaration in switch statement


Posted on 16th Feb 2014 07:03 pm by admin

Hello, I am having some trouble assigning a value to a variable inside a switch statement. What I am trying to do here is trip an error if the user has already added an item in the shopping cart. The While and Foreach loops work perfectly fine. The problem is that the value of variable $error does not get passed outside of the case. As a result, I cannot get the error message to display. I really need to get this to work and any help is appreciated. The code looks something like this:

Code: switch ($action) {
case 'add' :
{
if ($_SESSION['CartID'] == "")
{

$query = 'INSERT INTO ShopCart( UserID )VALUES("' . $UserID . '")';
mysql_query($query, $db) or die(mysql_error($db));

$Shop = 'SELECT ShopCartID FROM ShopCart WHERE UserID = "' . $UserID . '" ORDER BY ShopCartID ASC LIMIT 1';
$Cart = mysql_query($Shop);
$ShopCart = mysql_fetch_row($Cart);
$_SESSION['CartID'] = $ShopCart[0];
}
$query = 'SELECT ProductID FROM ShopCartLine WHERE ShopCartID = "' . $_SESSION['CartID'] . '"';
$result=mysql_query($query, $db) or die(mysql_error($db));

//$row = mysql_fetch_row( $result );
while($var = mysql_fetch_array($result))
{
foreach ($var as $i) {
if ($i == $ProductID) {
$error = 1; //TRIP THE ERROR HERE!
echo "<script type='text/javascript'>window.top.location='http://amarcy2.db-class.ids.uic.edu/shopcart.php';</script>";
}
}
}
}


The code being used to display the error message is as follows:

Code:
if ($error == 1) {
echo '<p>You already have this item in your basket!</p>';
}

No comments posted yet

Your Answer:

Login to answer
286 Like 15 Dislike
Previous forums Next forums
Other forums

Error in write byte array into document in SM58
Hi Gurus,

Getting the following error status in Transactional RFC (SM58) ..... *Error i

no idea what to do...
I'm fed up. I had issues with code that I have working on a different site that didn't work on my cu

Help: problem with Headers to download PDF file
Hi,

I'm trying to implement a script where when a user clicks a link he gets a PDF file to op

How to show more than 1 users with this code...
Hello,
i have a table that shows users only if I, as Administrator, want to be shown. But its sho

php code generators
All

Whilst enjoying learning a new language i have come accross a number of free code generat

Multipe Dynamic Controls & AutoPostback Issue
I currently have a need to create many dynamic controls (Example Textboxes) that need to do a PostBa

How to schedule the a job?
Hi,
I have prepared a task ( i.e. a procedure is prepare the file and send the same to con

this code is not working????
it says this error.. Warning: Division by zero in ..

the variables are correct, so why is th

frames get header location..
i have two frames, one top, one bottom. how do i do a form on the bottom frame that gives me the ur

Multiple upload and Resize
I would like some help on my script I have the for my index.php

////

<html&

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash