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
foreach loop, assistance request
I would like some guidance on the usage of foreach as I try to parse through a large database and wh
C problem struct and string
Hi guys,
This is my first post and I'm hoping the experts here can help me solve my problem.<
problem with array - multilingual page
this is my test page :
Code: <?php
if(isset($_GET['lang'])) {
if($_GET['la
If a record matches an existing record do nothing
So I don't know if I should put this here or in mysql, but what my script is for is for people to ad
Need help in Generating Combinations
Need help generating all possible combination of names in an array
Lets say i have the follow
Passing Arguments to execlp()
I'm writing a program that mimics a unix shell. It's supposed to take commands with arguments and ex
quick question about System Change Number(SCN) in FlashBack Query topic
i know tht we can get the SCN of the database using flashback concept as follows
SQL>
Why is this query failing?
Why is this not working?
$query = "SELECT * FROM `users` WHERE `userid` = " . $USERID
PHP Code To Change Font Color in Table Cell
I would appreciate help with the following snippet of my php code. I am just trying to change the f
Upload, SSL and more php help
I recently just installed a ssl cert and do i use https for the whole site or just for the checkout.