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 7 of 7
  1. #1
    Banned
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Shopping Cart - Not Working

    On executing the code below, an error message appears. Why is this?

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

    Code:
    $total = 0;
    echo '<table>';
    foreach ($contents as $id=>$qty) {
    $sql = 'SELECT * FROM books WHERE id = '.$id;
    $result = mysql_query($sql);
    
    $row = mysql_fetch_array($result);
    
    echo '<tr>';
    echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
    echo '<td>'.$row['title'].' 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>';
    
    echo writeShoppingCart();
    echo '<a href="cart.php?action=add&id=1">Add to cart</a>';
    Last edited by Cerby; 11-16-2007 at 11:18 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Add error checking to your query to find out what the real problem is.

    PHP Code:
    $result mysql_query($query);

    //ERROR CHECKING!!!!
    if (!$result) {
        die(
    "Query failed. Query text: $query<br />Error: ".mysql_error());


  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Shouldn't this:
    PHP Code:
    $sql 'SELECT * FROM books WHERE id = '.$id
    Look like this ?:
    PHP Code:
    $sql "SELECT * FROM books WHERE id = '$id'"

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Actually, no. Either way should work, but you shouldn't wrap integers in quotes.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Inigoesdr View Post
    Actually, no. Either way should work, but you shouldn't wrap integers in quotes.
    oh sorry didn't know, what's the reason not to do it though?

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Because when you put it in quotes it will be treated as a string, and that can cause problems when doing comparisons or inserts. Like I said, most of the time using quotes will work, but there's no reason to use them, and it's poor coding practice.

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,048
    Thanks
    2
    Thanked 317 Times in 309 Posts
    Also, by putting numeric values in quotes, making them strings, in a numeric column, forces mysql to do extra work to convert them back to the numeric value, unnecessarily slowing down the query and if the value is large enough or small enough can result in an inexact conversion. From the mysql manual -
    If a numeric expression contains any strings, they are converted to double-precision floating-point values and the expression is approximate.
    Short answer - only put quotes around strings.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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