By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

PHP- undefined Index

P: 3
Hi

i am having problems with some code that i wrote out for a shopping cart. the shopping cart it self works when a product is added but when the 'CART CONTENT' button is clicked on it comes up with an error. the error is

Notice: Undefined index: action in D:\www\students\khank\WebsiteFinal1SHAZAD\cart.php on line 5

the code that i have written for this is

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     include("database.php");
  4.  
  5.     switch($_GET["action"])
  6.     {
  7.         case "add_item":
  8.         {
  9.             AddItem($_GET["id"], $_GET["qty"]);
  10.             ShowCart();
  11.             break;
  12.         }
  13.         case "update_item":
  14.         {
  15.             UpdateItem($_GET["id"], $_GET["qty"]);
  16.             ShowCart();
  17.             break;
  18.         }
  19.         case "remove_item":
  20.         {
  21.             RemoveItem($_GET["id"]);
  22.             ShowCart();
  23.             break;
  24.         }
  25.         case "default":
  26.         {
  27.             ShowCart();
  28.         }
  29.     }
  30.  
  31.     function AddItem($productid, $qty)
  32.     {
  33.         // Will check whether or not this item
  34.         // already exists in the cart table.
  35.         // If it does, the UpdateItem function
  36.         // will be called instead
  37.  
  38.         global $host, $user, $pass;
  39.  
  40.         // Get a connection to the database
  41.         $cxn = @mysql_connect("$host", "$user", "$pass");
  42.  
  43.         $result = mysql_query("select count(*) from cart where cookieid = '" . GetCartId() . "'");
  44.         $row = mysql_fetch_row($result);
  45.         $numRows = $row[0];
  46.  
  47.         if($numRows == 0)
  48.         {
  49.             // This item doesn't exist in the users cart,
  50.             // we will add it with an insert query
  51.             @mysql_query("insert into cart(cookieid,productid,qty) values('" . GetCartId() . "',$productid,$qty)") or die(mysql_error());
  52.         }
  53.         else
  54.         {
  55.             // This item already exists in the users cart,
  56.             // we will update it instead
  57.  
  58.             UpdateItem($productid, $qty);
  59.         }
  60.     }
  61.  
  62.     function UpdateItem($productid, $qty)
  63.     {
  64.         // Updates the quantity of an item in the users cart.
  65.         // If the qutnaity is zero, then RemoveItem will be
  66.         // called instead
  67.  
  68.         global $host, $user, $pass;
  69.  
  70.         // Get a connection to the database
  71.         $cxn = @mysql_connect("$host", "$user", "$pass");
  72.  
  73.         if($qty == 0)
  74.         {
  75.             // Remove the item from the users cart
  76.             RemoveItem($productid);
  77.         }
  78.         else
  79.         {
  80.             mysql_query("update cart set qty = $qty where cookieid = '" . GetCartId() . "' and productid = $productid");
  81.         }
  82.     }
  83.  
  84.     function RemoveItem($productid)
  85.     {
  86.         // Uses an SQL delete statement to remove an item from
  87.         // the users cart
  88.  
  89.         global $host, $user, $pass;
  90.  
  91.         // Get a connection to the database
  92.         $cxn = @mysql_connect("$host", "$user", "$pass");
  93.  
  94.         mysql_query("delete from cart where cookieid = '" . GetCartId() . "' and productid = $productid");
  95.     }
  96.  
  97.     function ShowCart()
  98.     {
  99.         // Gets each item from the cart table and display them in
  100.         // a tabulated format, as well as a final total for the cart
  101.  
  102.         global $host, $user, $pass;
  103.  
  104.         // Get a connection to the database
  105.         $cxn = @mysql_connect("$host", "$user", "$pass");
  106.  
  107.         $totalCost = 0;
  108.         $result = mysql_query("select * from cart c join products p on c.productid = p.productid where c.cookieid = '" . GetCartId() . "'");
  109.         //$result = mysql_query("select * from cart c inner join products p on c.productid = p.productid");
  110.         ?>
  111.  
the error comes on line five with the 'SWITCH STATEMENT'. i don't know what to do. Can any one please help. it would be very much appreciated.
May 29 '07 #1
Share this Question
Share on Google+
8 Replies


Atli
Expert 5K+
P: 5,058
Hi, and welcome to TSDN.

This is not an really an error, it is a notice. It is displayed because the GET variable 'action' is empty, so when you attempt to read $_GET['action'] the 'action' field of the array doesn't exist. Ergo, you get a undefined index notice.
May 29 '07 #2

Atli
Expert 5K+
P: 5,058
I've edited the thread's title to better describe it's contents.

Please read the Posting Guidelines before posing.

MODERATOR
May 29 '07 #3

pbmods
Expert 5K+
P: 5,821
You can turn off this notice by either editing your php.ini file, or by calling:

Expand|Select|Wrap|Line Numbers
  1. error_reporting(~E_NOTICE);
http://www.php.net/manual/en/ref.err...rror-reporting
May 29 '07 #4

P: 3
hi
im still having problems with resolving this issue, anyone know of any solutions.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     include("database.php");
  4.  
  5.     switch($action)
  6.     {
  7.         case "add_item":
  8.         {
  9.             AddItem($_GET["id"], $_GET["qty"]);
  10.             ShowCart();
  11.             break;
  12.         }
  13.         case "update_item":
  14.         {
  15.             UpdateItem($_GET["id"], $_GET["qty"]);
  16.             ShowCart();
  17.             break;
  18.         }
  19.         case "remove_item":
  20.         {
  21.             RemoveItem($_GET["id"]);
  22.             ShowCart();
  23.             break;
  24.         }
  25.         case "default":
  26.         {
  27.             ShowCart();
  28.         }
  29.     }
  30.  
  31.     function AddItem($productid, $qty)
  32.     {
  33.         // Will check whether or not this item
  34.         // already exists in the cart table.
  35.         // If it does, the UpdateItem function
  36.         // will be called instead
  37.  
  38.         global $host, $user, $pass;
  39.  
  40.         // Get a connection to the database
  41.         $cxn = @mysql_connect("$host", "$user", "$pass");
  42.  
  43.         $result = mysql_query("select count(*) from cart where cookieid = '" . GetCartId() . "'");
  44.         $row = mysql_fetch_row($result);
  45.         $numRows = $row[0];
  46.  
  47.         if($numRows == 0)
  48.         {
  49.             // This item doesn't exist in the users cart,
  50.             // we will add it with an insert query
  51.             @mysql_query("insert into cart(cookieid,productid,qty) values('" . GetCartId() . "',$productid,$qty)") or die(mysql_error());
  52.         }
  53.         else
  54.         {
  55.             // This item already exists in the users cart,
  56.             // we will update it instead
  57.  
  58.             UpdateItem($productid, $qty);
  59.         }
  60.     }
  61.  
  62.     function UpdateItem($productid, $qty)
  63.     {
  64.         // Updates the quantity of an item in the users cart.
  65.         // If the qutnaity is zero, then RemoveItem will be
  66.         // called instead
  67.  
  68.         global $host, $user, $pass;
  69.  
  70.         // Get a connection to the database
  71.         $cxn = @mysql_connect("$host", "$user", "$pass");
  72.  
  73.         if($qty == 0)
  74.         {
  75.             // Remove the item from the users cart
  76.             RemoveItem($productid);
  77.         }
  78.         else
  79.         {
  80.             mysql_query("update cart set qty = $qty where cookieid = '" . GetCartId() . "' and productid = $productid");
  81.         }
  82.     }
  83.  
  84.     function RemoveItem($productid)
  85.     {
  86.         // Uses an SQL delete statement to remove an item from
  87.         // the users cart
  88.  
  89.         global $host, $user, $pass;
  90.  
  91.         // Get a connection to the database
  92.         $cxn = @mysql_connect("$host", "$user", "$pass");
  93.  
  94.         mysql_query("delete from cart where cookieid = '" . GetCartId() . "' and productid = $productid");
  95.     }
  96.  
  97.     function ShowCart()
  98.     {
  99.         // Gets each item from the cart table and display them in
  100.         // a tabulated format, as well as a final total for the cart
  101.  
  102.         global $host, $user, $pass;
  103.  
  104.         // Get a connection to the database
  105.         $cxn = @mysql_connect("$host", "$user", "$pass");
  106.  
  107.         $totalCost = 0;
  108.         $result = mysql_query("select * from cart c join products p on c.productid = p.productid where c.cookieid = '" . GetCartId() . "'");
  109.         //$result = mysql_query("select * from cart c inner join products p on c.productid = p.productid");
  110.         ?>
[Please use CODE tags when posting source code. Thanks! --pbmods]
May 31 '07 #5

P: 3
me is still having problems, any got any solutions???????????????
May 31 '07 #6

Expert 10K+
P: 11,448
I found this question in the Editor's Corner; I think this forum is a more appropriate place for it.

kind regards,

Jos
May 31 '07 #7

pbmods
Expert 5K+
P: 5,821
Merged duplicate threads.
May 31 '07 #8

pbmods
Expert 5K+
P: 5,821
You can turn off this notice by either editing your php.ini file, or by calling:

Expand|Select|Wrap|Line Numbers
  1. error_reporting(~E_NOTICE);
http://www.php.net/manual/en/ref.err...rror-reporting
May 31 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.