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); }
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...
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...