PHP- undefined Index

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seni786
    New Member
    • May 2007
    • 3

    PHP- undefined Index

    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\WebsiteF inal1SHAZAD\car t.php on line 5

    the code that i have written for this is

    [code=php]
    <?php

    include("databa se.php");

    switch($_GET["action"])
    {
    case "add_item":
    {
    AddItem($_GET["id"], $_GET["qty"]);
    ShowCart();
    break;
    }
    case "update_ite m":
    {
    UpdateItem($_GE T["id"], $_GET["qty"]);
    ShowCart();
    break;
    }
    case "remove_ite m":
    {
    RemoveItem($_GE T["id"]);
    ShowCart();
    break;
    }
    case "default":
    {
    ShowCart();
    }
    }

    function AddItem($produc tid, $qty)
    {
    // Will check whether or not this item
    // already exists in the cart table.
    // If it does, the UpdateItem function
    // will be called instead

    global $host, $user, $pass;

    // Get a connection to the database
    $cxn = @mysql_connect( "$host", "$user", "$pass");

    $result = mysql_query("se lect count(*) from cart where cookieid = '" . GetCartId() . "'");
    $row = mysql_fetch_row ($result);
    $numRows = $row[0];

    if($numRows == 0)
    {
    // This item doesn't exist in the users cart,
    // we will add it with an insert query
    @mysql_query("i nsert into cart(cookieid,p roductid,qty) values('" . GetCartId() . "',$productid,$ qty)") or die(mysql_error ());
    }
    else
    {
    // This item already exists in the users cart,
    // we will update it instead

    UpdateItem($pro ductid, $qty);
    }
    }

    function UpdateItem($pro ductid, $qty)
    {
    // Updates the quantity of an item in the users cart.
    // If the qutnaity is zero, then RemoveItem will be
    // called instead

    global $host, $user, $pass;

    // Get a connection to the database
    $cxn = @mysql_connect( "$host", "$user", "$pass");

    if($qty == 0)
    {
    // Remove the item from the users cart
    RemoveItem($pro ductid);
    }
    else
    {
    mysql_query("up date cart set qty = $qty where cookieid = '" . GetCartId() . "' and productid = $productid");
    }
    }

    function RemoveItem($pro ductid)
    {
    // Uses an SQL delete statement to remove an item from
    // the users cart

    global $host, $user, $pass;

    // Get a connection to the database
    $cxn = @mysql_connect( "$host", "$user", "$pass");

    mysql_query("de lete from cart where cookieid = '" . GetCartId() . "' and productid = $productid");
    }

    function ShowCart()
    {
    // Gets each item from the cart table and display them in
    // a tabulated format, as well as a final total for the cart

    global $host, $user, $pass;

    // Get a connection to the database
    $cxn = @mysql_connect( "$host", "$user", "$pass");

    $totalCost = 0;
    $result = mysql_query("se lect * from cart c join products p on c.productid = p.productid where c.cookieid = '" . GetCartId() . "'");
    //$result = mysql_query("se lect * from cart c inner join products p on c.productid = p.productid");
    ?>
    [/code]

    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.
    Last edited by Atli; May 29 '07, 05:52 PM. Reason: Added code tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I've edited the thread's title to better describe it's contents.

      Please read the Posting Guidelines before posing.

      MODERATOR

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        You can turn off this notice by either editing your php.ini file, or by calling:

        [code=php]error_reporting (~E_NOTICE);[/code]

        Comment

        • seni786
          New Member
          • May 2007
          • 3

          #5
          PHP- undefined Index

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

          [code=php]
          <?php

          include("databa se.php");

          switch($action)
          {
          case "add_item":
          {
          AddItem($_GET["id"], $_GET["qty"]);
          ShowCart();
          break;
          }
          case "update_ite m":
          {
          UpdateItem($_GE T["id"], $_GET["qty"]);
          ShowCart();
          break;
          }
          case "remove_ite m":
          {
          RemoveItem($_GE T["id"]);
          ShowCart();
          break;
          }
          case "default":
          {
          ShowCart();
          }
          }

          function AddItem($produc tid, $qty)
          {
          // Will check whether or not this item
          // already exists in the cart table.
          // If it does, the UpdateItem function
          // will be called instead

          global $host, $user, $pass;

          // Get a connection to the database
          $cxn = @mysql_connect( "$host", "$user", "$pass");

          $result = mysql_query("se lect count(*) from cart where cookieid = '" . GetCartId() . "'");
          $row = mysql_fetch_row ($result);
          $numRows = $row[0];

          if($numRows == 0)
          {
          // This item doesn't exist in the users cart,
          // we will add it with an insert query
          @mysql_query("i nsert into cart(cookieid,p roductid,qty) values('" . GetCartId() . "',$productid,$ qty)") or die(mysql_error ());
          }
          else
          {
          // This item already exists in the users cart,
          // we will update it instead

          UpdateItem($pro ductid, $qty);
          }
          }

          function UpdateItem($pro ductid, $qty)
          {
          // Updates the quantity of an item in the users cart.
          // If the qutnaity is zero, then RemoveItem will be
          // called instead

          global $host, $user, $pass;

          // Get a connection to the database
          $cxn = @mysql_connect( "$host", "$user", "$pass");

          if($qty == 0)
          {
          // Remove the item from the users cart
          RemoveItem($pro ductid);
          }
          else
          {
          mysql_query("up date cart set qty = $qty where cookieid = '" . GetCartId() . "' and productid = $productid");
          }
          }

          function RemoveItem($pro ductid)
          {
          // Uses an SQL delete statement to remove an item from
          // the users cart

          global $host, $user, $pass;

          // Get a connection to the database
          $cxn = @mysql_connect( "$host", "$user", "$pass");

          mysql_query("de lete from cart where cookieid = '" . GetCartId() . "' and productid = $productid");
          }

          function ShowCart()
          {
          // Gets each item from the cart table and display them in
          // a tabulated format, as well as a final total for the cart

          global $host, $user, $pass;

          // Get a connection to the database
          $cxn = @mysql_connect( "$host", "$user", "$pass");

          $totalCost = 0;
          $result = mysql_query("se lect * from cart c join products p on c.productid = p.productid where c.cookieid = '" . GetCartId() . "'");
          //$result = mysql_query("se lect * from cart c inner join products p on c.productid = p.productid");
          ?>[/code]

          [Please use CODE tags when posting source code. Thanks! --pbmods]
          Last edited by pbmods; May 31 '07, 03:16 PM. Reason: Added code tags.

          Comment

          • seni786
            New Member
            • May 2007
            • 3

            #6
            me is still having problems, any got any solutions?????? ?????????

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              I found this question in the Editor's Corner; I think this forum is a more appropriate place for it.

              kind regards,

              Jos

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Merged duplicate threads.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  You can turn off this notice by either editing your php.ini file, or by calling:

                  [code=php]error_reporting (~E_NOTICE);[/code]

                  Comment

                  Working...