1

Everything is working file but the cash is not updating. I think its due to typecasting. The value for shares is an integer and price is a float-point integer but I'm confused as to how to cast them.

Code:

// deletes the user's stock own and update the cash balance.
$stock = lookup($_POST["stocks"]);

// get how much a person own ( cash value of stock ) 
if ( $stock !== false )
{
    $shares = query("SELECT shares FROM stocks WHERE id = ?",$_SESSION["id"] ) ;
    $stockvalue = number_format($shares["shares"],2,'.','')
                                       * number_format($stock["price"], 2, '.', '') ;

    //update the current cash and delete the stocks 
    query("DELETE FROM stocks WHERE id = ?",$_SESSION["id"] );

    query("UPDATE users SET cash = cash + ? WHERE id = ?",$stockvalue, $_SESSION["id"]);

    redirect("index.php");
}
4
  • Have you checked that your stockvalue variable is calculating the right number? Try removing the number formatting.
    – lethaljd
    Nov 10, 2014 at 1:33
  • Problem is now solved thank you. Nov 11, 2014 at 18:02
  • If you found the answer, you can answer the question yourself and mark your answer as accepted so this question may be of use to other readers. Otherwise, you can delete the question.
    – lethaljd
    Nov 12, 2014 at 21:18
  • Wouldn't this assume that you have only one row of shares? Nov 22, 2014 at 23:17

0

You must log in to answer this question.

Browse other questions tagged .