Skip to main content
1 of 2

pset7 index, portfolio table problem

There are two problems I have been mulling over... Hopefully someone with a fresh perspective will catch my mistakes!

Q1. The first is that the ID from the Users table is going into the ID column of the Portfolio table, not the Users_id column. I feel like this has to do with my INSERT query in Buy and Sell, so here they are:

For buy:

    CS50::query("INSERT INTO portfolio (user_id, symbol, shares) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)", $_SESSION["id"], $symbol, $_POST["shares"]);

For Sell:

    CS50::query("DELETE FROM portfolio WHERE user_id = ? AND symbol = ?", $_SESSION["id"], $symbol);

Q2. This is my code for getting the balance:

$users = CS50::query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);

$balance = $users[0]["cash"];

render("balance" => $balance]);

I read that you have to do $users[0]["cash"] because query gets an array. But when I do Buy or Sell, all the shares go into a row where ID and User_id are both 0.

I've followed the walkthrough pretty closely, so I'm not sure where I'm stumbling. Thanks for any help at all :)