Delete problem...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vozzek
    New Member
    • Sep 2007
    • 40

    #1

    Delete problem...

    Hi all,

    I've got some php code running an SQL delete to remove rows from my shopping cart table (tbl_cart). It works on individual cart id's, but not when I want the entire cart emptied.

    tbl_cart looks like this:
    ct_id (cart id, this is the primary key)
    pd_id (product id)
    ct_qty (quantity)
    ct_session_id (user's unique session id)
    etc...

    Here's the code:


    [PHP]<?php
    // current session id
    $sid = session_id();

    // $cid passed from view_cart page
    // if $cid=0, then empty cart

    if ($cid != 0) {
    $sql = "DELETE FROM tbl_cart
    WHERE ct_id = $cid"; // This works
    } else {
    $sql = "DELETE FROM tbl_cart
    WHERE ct_session_id = $sid"; // This DOESN'T work
    }
    $result = mysql_query($sq l) or die(mysql_error ());
    ?>[/PHP]

    If I pass $cid as a whole number, it deletes that row - effectively deleting that item from the cart. But when the user presses the EMPTY CART button, I call the above code by passing $cid as zero. At this point I want all records deleted that match the current user's session id, which is $sid.

    The error I'm getting is:

    Unknown column '785fdf3a1a755e 2a2189fa3028f33 f22' in 'where clause'

    Thanks in advance.
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by vozzek
    Hi all,

    I've got some php code running an SQL delete to remove rows from my shopping cart table (tbl_cart). It works on individual cart id's, but not when I want the entire cart emptied.

    tbl_cart looks like this:
    ct_id (cart id, this is the primary key)
    pd_id (product id)
    ct_qty (quantity)
    ct_session_id (user's unique session id)
    etc...

    Here's the code:


    [PHP]<?php
    // current session id
    $sid = session_id();

    // $cid passed from view_cart page
    // if $cid=0, then empty cart

    if ($cid != 0) {
    $sql = "DELETE FROM tbl_cart
    WHERE ct_id = $cid"; // This works
    } else {
    $sql = "DELETE FROM tbl_cart
    WHERE ct_session_id = $sid"; // This DOESN'T work
    }
    $result = mysql_query($sq l) or die(mysql_error ());
    ?>[/PHP]

    If I pass $cid as a whole number, it deletes that row - effectively deleting that item from the cart. But when the user presses the EMPTY CART button, I call the above code by passing $cid as zero. At this point I want all records deleted that match the current user's session id, which is $sid.

    The error I'm getting is:

    Unknown column '785fdf3a1a755e 2a2189fa3028f33 f22' in 'where clause'

    Thanks in advance.
    This is beacuse the session id value that is passed is very large as shown above.

    Does the table has user_id column?
    I would suggest you to use user_id instaed of session_id

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      If ct_session_id is of type VARCHAR/CHAR then use single quotes around value i.e.
      [PHP]$sql = "DELETE FROM tbl_cart
      WHERE ct_session_id = '$sid'";[/PHP]

      Comment

      Working...