Problem with VARS

Discussion in 'PHP' started by afonseca, May 31, 2006.

  1. #1
    Hi!
    In my database, I have a table with a field (code) that keeps to codes of the type longtext like this:
    456-678
    335-667
    Etc.

    When I print in php it prints correctly therefore since it is a string. The problem starts when I have a function (it follows below the code) that it receives that variable and orders to insert on another table, it presents a different result in the database deducting the numbers.
    Example: 456-678 it keeps -222 therefore deducting 456 for 678 gives -222, and I wanted that it 456-678.
    I already made one gettype and it returns a string and it he returns one to me string, I don’t know because he makes this deducting.
    Somebody perceives what it happening?
    Thanks in advance for any helps.
    Sorry also for my English. If I didn’t make my self clear, let me know.

    António Fonseca

    -----------------------------

    function AddItem($itemId, $qty, $cod)
    {

    global $dbServer, $dbUser, $dbPass, $dbName;

    $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

    $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId ");
    $row = mysql_fetch_row($result);
    $numRows = $row[0];

    if($numRows == 0)
    {

    @mysql_query("insert into cart(cookieId, itemId, qty, cod) values('" . GetCartId() . "', $itemId, $qty, $cod)");
    }
    else
    {

    UpdateItem($itemId, $qty);
    }
     
    afonseca, May 31, 2006 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If your database column is a string, then when referring to that column's values in SQL you have to use single quotes. What's happening in your code is that your giving SQL values without quotes: therefore, SQL assumes something other than a string, sees an equation and works that out.

    Replace:
    @mysql_query("insert into cart(cookieId, itemId, qty, cod) values('" . GetCartId() . "', $itemId, $qty, $cod)");

    with:
    @mysql_query("insert into cart(cookieId, itemId, qty, cod) values('" . GetCartId() . "', $itemId, $qty, '$cod')");

    and see how that goes...
     
    TwistMyArm, May 31, 2006 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If your database column is a string, then when referring to that column's values in SQL you have to use single quotes. What's happening in your code is that your giving SQL values without quotes: therefore, SQL assumes something other than a string, sees an equation and works that out.

    Replace:
    @mysql_query("insert into cart(cookieId, itemId, qty, cod) values('" . GetCartId() . "', $itemId, $qty, $cod)");

    with:
    @mysql_query("insert into cart(cookieId, itemId, qty, cod) values('" . GetCartId() . "', $itemId, $qty, '$cod')");

    and see how that goes...
     
    TwistMyArm, May 31, 2006 IP
  4. afonseca

    afonseca Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot! Now it works fine!

    Best regards
    António Fonseca
     
    afonseca, May 31, 2006 IP