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

Count number of records in all the tables with a querry URGENT PLEASE...
I downloaded ORACLE 9I DATA DUMP into my comp. There are number of tables. I need to find out the

help connecting a form to php and then emailing the form
Ok. So, I made a form, and I need help to where when submitted, I get an email with the submitted de

Help adding strtolower( to my in_array
strtolower i need to add it to this to work, iuno how.

Code: $moderators = array('Admin', 'ba

Generate PDF
Hi guys,

I would like to know if there is a way to generate pdf when clicking on a link. In f

Multiple while loops
I have several DB queries that I know should be returning results and aren't. I have a feeling it ha

mysq_num_rows
Hi,

I want to use:

Code: if(mysql_num_rows($ergebnis2)==2)
how can the value 2, be

list files from folder, only one for each date
I have many files in a folder and wish to only list one of each date

these are the file names

division gives infinity anser
int main()
{
int z=0;
int i=1/z;
cout<
}

It doesn't throw a

Help With Showing Users On the Index Page
Ive got this code which works just how i want it to.

Code: <?
$timenow=time();

Hashing?
Hi, I was hoping to take a string value then convert it - consistently - across each of our pages in

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