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
15
Other forums
PHP mail() rejected by SMTP
I'm currently working on a newsletter application which is installed on a windows 2003 server runnin
Find current logon time
Hi,
in my sql script i want to query and find out the logon time of the current session.
<
making web pages for accounts
Hello, I would like to make pages for accounts on my website im making (its not a real website..im j
LSB (PHP 5.3) problem with static value!
hello,
i'm having a problem. static::$text variable gets lost at some point. can someone plea
How do i use vars in an array
Hiya peeps!
How do I use a var in an array. You will see $id but it isnt working.
use an array from 1 class in a second class
I have a class that adds data to an array
Then I have a second class and I need to check if t
OPINIONS WANTED
This is my login page code, and I want your opinion on it please!
Code: // Login ~ CHECKS
do while...woes
I'm sure this is childsplay but it is stumping me, everytime i try what I think would make this work
validating url
im trying to validate url's sent to me by a form
the url's im collecting are for placing banners
problems with script
I made a small script wich exchanges points in my website:
<?php
session_start