PHP Shopping basket - undefined index

Discussion in 'PHP' started by siyah miyah, Apr 28, 2008.

  1. #1
    This is my cart.php file with the error:

    Notice: Undefined index: prod_id in /**********************/*****/****/*******/******/******/web/fsbasket/cart.php on line 20

    prod_id is the reference to the column in my database....so in my query I am calling on the prod_id for the shopping basket....Can someone tell me why I get this msg?

    Is there a problem with connecting to the database perhaps?

    <?php
    // Include MySQL class
    include("mysql.class.php");
    // Include database connection
    include("global.inc");
    // Include functions
    include("functions.inc");
    // Start the session

    session_start();

    // Process actions
    $cart = $_SESSION['cart'];
    $action = $_GET['action'];
    switch ($action) {
    case 'add':
    if ($cart) {
    $cart .= ','.$_GET['prod_id'];
    } else {
    $cart = $_GET['prod_id'];
    }
    break;
    case 'delete':
    if ($cart) {
    $items = explode(',',$cart);
    $newcart = '';
    foreach ($items as $item) {
    if ($_GET['prod_id'] != $item) {
    if ($newcart != '') {
    $newcart .= ','.$item;
    } else {
    $newcart = $item;
    }
    }
    }
    $cart = $newcart;
    }
    break;
    case 'update':
    if ($cart) {
    $newcart = '';
    foreach ($_POST as $key=>$value) {
    if (stristr($key,'qty')) {
    $prod_id = str_replace('qty','',$key);
    $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
    $newcart = '';
    foreach ($items as $item) {
    if ($prod_id != $item) {
    if ($newcart != '') {
    $newcart .= ','.$item;
    } else {
    $newcart = $item;
    }
    }
    }
    for ($i=1;$i<=$value;$i++) {
    if ($newcart != '') {
    $newcart .= ','.$prod_id;
    } else {
    $newcart = $prod_id;
    }
    }
    }
    }
    }
    $cart = $newcart;
    break;
    }
    $_SESSION['cart'] = $cart;
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Shopping Basket</title>
    <link rel="stylesheet" href="styles.css" />
    </head>

    <body>

    <div id="shoppingcart">

    <h1>Your Shopping Basket</h1>

    <?php
    echo writeShoppingCart();
    ?>

    </div>

    <div id="contents">

    <h1>Please check quantities</h1>

    <?php
    echo showCart();
    ?>

    <p><a href="index.php">Return to homepage</a></p>

    </div>

    </body>
    </html>

    Thanks, in advance, for any responses!
     
    siyah miyah, Apr 28, 2008 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    Its a notice. You can turn it off.
    Put this in top of your PHP script.
    
    error_reporting(E_ALL ^ E_NOTICE);
    
    PHP:
    Or when you try to get something from post / get, use this :
    
    $name = '';
    
    //
    // You want to read it from $_GET
    //
    if (isset($_GET['name'])) {
       $name = $_GET['name'];
    }
    
    PHP:
     
    xrvel, Apr 28, 2008 IP
  3. siyah miyah

    siyah miyah Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your response.

    What it really comes down to is..when I try pull data from the database i set them in the code (illustrated below):

    $output[] = '<tr>';
    $output[] = '<td><form action="cart.php?action=delete&prod_id=.'$id.'" method="post" id="Delete">Remove</a></td>';
    $output[] = '<td>'$contents['p_name']'</td>'; //'.$p_name.'
    $output[] = '<td>&pound;'.$price_wk.'</td>';
    $output[] = '<td><input type="text" name="qty '.$id.' "value="'.$qty.'" size="3" maxlength="3" /></td>';
    $output[] = "<td>&pound;".($price_wk * $qty)."</td>";
    $total += $price_wk * $qty;
    $output[] = '</tr>';

    I get an undefined variable error. Which relates to $..which is the name of the attribute I am trying to extract from the database....

    Must I define these variables. I thought that since it was coming from the database it was not necessary. PLEASE correct me.

    If so, would I just have to define it as stated above using the $name=''; and if(isset)? I had tried this earlier, setting it in my cart.php file and it still did not work...should it be in the functions.inc file?
     
    siyah miyah, Apr 28, 2008 IP
  4. Synchronium

    Synchronium Active Member

    Messages:
    463
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Check your DB column names.
     
    Synchronium, Apr 28, 2008 IP
  5. siyah miyah

    siyah miyah Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks.

    DB column names are exactly as stated.

    Could it be a problem with my DB connection?
     
    siyah miyah, Apr 28, 2008 IP
  6. siyah miyah

    siyah miyah Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I use action to refer to the delete, add items, etc. This can be seen in full in the first post. I keep getting the error:

    Notice: Undefined index: action in /*************************************************fsbasket/cart.php on line 67

    But hasn't this already been defined with the $action = $_GET['action']; ?

    Also for this line $result = $db->query($sql);
    I get this error:
    Fatal error: Call to a member function query() on a non-object in /*****************************************fsbasket/cart.php on line 37

    What does this mean and how do I fix it?
     
    siyah miyah, Apr 28, 2008 IP