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

PHP template help
Ok, I don't know if anyone can help me but I am using PHP templates to make a site. I am a newbie an

Functions Not Loading Into Div
I had some help doing some of this but what I'm trying to do is get my functions to retrieve its val

extract content from a website
i have written a code that will grab the content from the index page..
i would like to know how c

Find current logon time
Hi,
in my sql script i want to query and find out the logon time of the current session.
<

PHP5 - AJAX help
I've been following the tutorial on w2schools (http://www.w3schools.com/php/php_ajax_database.asp) a

Force download script not handling files with spaces properly
I have a regular old php force download script, uses this code:

Code: header("Cache-Cont

Multiple Do / While Statements?
I'm still very new to PHP, and running in to a problem when trying to execute a do/while loop inside

Php Mysql Page Loading Notice
Hi all,

I was wondering if there is a way (Sure there is) of showing a message or an image wh

mysql UPDATE request not working and driving me crazy!!!
Hi,

I have been stuck on this for ages.

Quite simply I am trying to update my database

if statements problems
Hi. I'm trying to make a web form, but I kind of hit a dead end trying to figure out why it doesn't

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