Jump to content

Help me with this undefined variable errors


co.ador

Recommended Posts

I was wondering where does the 'user_cart' in the session variable is coming from?

 

there are a couple of error that comes up in the browser in relation to the user_cart index being undefined

 

with the code below I get the following error please help....

 

Notice: Undefined index: action in C:\wamp\www\shoes\stores\cart.php on line 15

 

Notice: Undefined variable: _SESSION in C:\wamp\www\shoes\stores\cart.php on line 91

 

Notice: Undefined variable: _SESSION in C:\wamp\www\shoes\stores\cart.php on line 93

 

Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\shoes\stores\cart.php on line 93

 

Notice: Undefined variable: _SESSION in C:\wamp\www\shoes\stores\cart.php on line 98

 

Notice: Undefined variable: numRows in C:\wamp\www\shoes\stores\cart.php on line 48

 

Notice: Undefined variable: itemId in C:\wamp\www\shoes\stores\cart.php on line 53

 

Notice: Undefined variable: qty in C:\wamp\www\shoes\stores\cart.php on line 53

Inserted records: -1

 

<?php
include("db.php");
ini_set( "display_errors", 0);
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$qty   = $_POST['qty'];
}
switch($_GET["action"]) //line 15
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_POST["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}

}
function AddItem($itemId, $qty)
{
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0) // undefine variable numRows variable... line 48 "crazy editor lol.." 
{
// This item doesn't exist in the users cart,

// we will add it with an insert query
mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");

printf ("Inserte records: %d\n", mysql_affected_rows());
}
else
{
// This item already exists in the users cart,

// we will update it instead
UpdateItem($itemId, $qty); // line 53 undefine $itemId and $qty variables help please....
}
}
function UpdateItem($itemId, $qty)
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
printf ("Updated records: %d\n", mysql_affected_rows());
}
function RemoveItem($itemId)
{
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
?>
<?
function ShowCart()
{
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
$user    = $_SESSION['user_cart'];
      $user_cart      = implode(",",$_SESSION['user_cart']); // line implode invalid argument...
$result1 = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
$line = mysql_fetch_array($result1, MYSQL_ASSOC);
   if (!$_SESSION['user_cart']) { // line 91
         $_SESSION['user_cart'] = array(); // line 93
  }
   $_SESSION['user_cart'][]['itemId'] = $line['itemId'];
   $_SESSION['user_cart'][]['itemName'] = $line['itemName'];
   $_SESSION['user_cart'][]['itemPrice'] = $line['itemPrice']; // line 98 most of the $_session variables are undifine I think it's the 'user_cart' index that is not define help...
   header("Location:cart2.php");
while($row = mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<form action="cart.php?action=update_item" method="post">
  <fieldset>
    <legend>Update quantity</legend>
    <input type="text" name="qty">
    <input type="submit" value="Update Cart">
  </fieldset>
</form>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<br>
<?php
}
?>
<? } ?>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.value
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.