I understand that I need a portfolio for each user, to contain their stocks and info, but how do I give each user a portfolio in the phpliteadmin? I know how to make one table, but I would need one "nested" or something inside the user DB or something like that? I'm just not sure where to put it, thanks


No, a single portfolio table is all you need. The secret is to have the correct columns and, more importantly, the correct unique index or key.

The table needs the following columns: userid, stock, quantity. You can add more columns, if they serve a useful purpose. Now, the magic sauce. There needs to be a unique key or index on the combination of the two columns userid and stock. This guarantees that there is only one row for the combination, for example, user23 and AAPL, but will still allow another entry for user23 and GOOG. When the number of shares of a certain stock for a certain user is increased or decreased, the row is updated, instead of adding a new row. Sell all of the shares? Delete the row.

"But how do I put all users in the same table?" you might ask? Well, when you want a single user's portfolio, you just need to select all rows where the userid matches the one you want.

It's really not that hard to do. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • So sell would look something like (in pseudocode) select rows with user23 as id, and goog as stock, sell x of them. And would Buy be like: If user and stock doesn't exist, make it, and buy x, and if it did exist, add x of them. How would I do that conditional?
    – Joel Banks
    Nov 16 '18 at 2:02
  • At this point in the class, the expectation is that you have the skills to research these things. However, I'll give you some hints. Your pseudocode is on track. The SQL commands UPDATE, INSERT and ON DUPLICATE are your friends. ;-)
    – Cliff B
    Nov 16 '18 at 2:41
  • If you're satisfied with the answer, please click on the check mark. Otherwise, it'll sit in the unanswered question pool forever. thanks.
    – Cliff B
    Nov 16 '18 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .