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
Mail Script Problems
I have created a PHP mail script. I am having a problem though. In $body i am trying to put two va
reating a background image
I am building a site in drupal and have a php form in it, due to certain annoyances with module buil
Help with some dates
I have a list of dates in an array:
$mondays = array(
strtotime("October 12, 2009
MySQL Does not UPDATE- SQLString Problem
vb Syntax (Toggle Plain Text)
1.
SQL = "UPDATE sampletable SET column1 = 'C1sample1'"
How to display value in drop down list after form has been reloaded
Please bear with me as I am very new to php and html.
I have a form with several drop down me
Log $_POST
How would i log submits on a form, by everyone? I want to then echo the number of submissions.
Transport data between itab and textfield on ALV event
Hallo,
I have a ALV Grid ( cl_gui_alv_grid ) and I also have hotspot click event with a h
Why is this function returning a false value when it shouldn't be??
This is in an include file. I want it to check a value in an html form and see if it's just white s
Set Categories
I am creating a submission form for somebody to come in and submit products. These products are sep
How to find OS bit version
How can I tell the person browsing my site is running 32, or 64 bit operating system?