Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Banned
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem wth Code

    Why do I get the following error message(s), when executing the following code:?

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\User\Desktop\Xampp\htdocs\cart.php on line 46

    Code:
    <?php
    
    session_start();
    $con = mysql_connect("localhost","ODBC","");
    if (!$con)
    {
      die(mysql_error());
    }
    
    function writeShoppingCart() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
    return '<p>You have no items in your shopping cart</p>';
    } else {
    // Parse the cart session variable
    $items = explode(',',$cart);
    $s = (count($items) > 1) ? 's':'';
    return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
    }
    }
    
    $cart = $_SESSION['cart'];
    if ($cart) {
    $cart .= ','.$_GET['id'];
    } else {
    $cart = $_GET['id'];
    }
    $_SESSION['cart'] = $cart;
    
    
    $cart = $_SESSION['cart'];
    if ($cart) {
    $items = explode(',',$cart);
    $contents = array();
    foreach ($items as $item) {
    $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    }
    }
    
    $total = 0;
    echo '<table>';
    
    foreach ($contents as $id=>$qty) {
    
    $result = mysql_query("SELECT * FROM books WHERE id = '$cart'");
    $row = mysql_fetch_assoc($result);
    echo $row['title'];
    echo $row['price']; 
    
    
    echo '<tr>';
    echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
    echo '<td>'.$author.' by '.$author.'</td>';
    echo '<td>&pound;'.$price.'</td>';
    echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    echo '<td>&pound;'.($price * $qty).'</td>';
    $total += $price * $qty;
    echo '</tr>';
    }
    echo '</table>';
    echo '<p>Grand total: &pound;'.$total.'</p>';
    
    ?>
    Last edited by Cerby; 11-18-2007 at 01:21 PM.

  • #2
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    //$result = mysql_query("SELECT * FROM books WHERE id = '$cart'");

    // Use 
    $result mysql_query("SELECT * FROM books WHERE id = '$id'"); 
    -------------------------
    Shailesh Patil
    -------------------------


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •