Pset7 buy. Debug is super clear but I still don’t understand why it doesn’t work:

DEBUG:cs50:SELECT cash FROM users WHERE id = 1 DEBUG:cs50:INSERT INTO portfolio (stock, shares, balance) VALUES(NULL, '2', NULL) DEBUG:cs50:UPDATE users SET cash = balance WHERE id = NULL

This is my code https://pastebin.com/MHDb9SVP

Maybe there is a problem with my table

enter image description here

2 Answers 2


I can see a couple of things that you may consider changing and also most of your code is correct,so just few things to add on!

The code where you are inserting values after checking that cash > stock in the else part as below :

               session["user_id"]=db.execute("INSERT INTO portfolio (stock, shares, 
               balance) VALUES(:stock, :shares, :balance)",
               stock = request.form.get("stock"), shares =request.form.get("shares"),
               balance = request.form.get("balance"))
               users = db.execute("UPDATE users SET cash = balance WHERE id = :id", 
               id = session["user_id"])

Here there are three variables shares,stock,balance and shares = int(request.form.get("shares")) whereas stock and balance are not a part of your form as in the form there are only two var shares and symbol. So, change stock and balance to the stock and balance variable you declared earlier in your program which were stock = price*shares balance = cash - stock.Also, add the user_id into your insert statement to insert each row for each user. You may capitalize the stock,balance var for use in the insert statement so as to differentiate it from the local previous stock,balance vars you were using as :

        db.execute("INSERT INTO portfolio (user_id,stock, shares, balance) VALUES(:user_id,:Stock, :shares, :Balance)",user_id=session["user_id"],
        stock = request.form.get("stock"), Shares = shares,
        Balance = balance)
        users = db.execute("UPDATE users SET cash = balance WHERE id = :id", id = session["user_id"])

If this helps, please give a checkmark!

  • Thanks a lot! Yes, I added user_id to INSERT and it finally started inserting the information. But I still have the problem with NULL. Stock, balance and symbol are NULL. I don’t really understand where it’s coming from. You explained what I should change there but it’s kinda confusing. Oct 19, 2018 at 8:44
  • Take a look into my second part of the answer that clearly shows why those values are not coming as expected.
    – sk.76
    Oct 19, 2018 at 8:47
  • It's like just change a bit of code as I am explaining below again.
    – sk.76
    Oct 19, 2018 at 8:49
  • Thanks a lot!!!!!! It’s working! Oct 19, 2018 at 8:53
  • 1
    Strongly disagree with "primary key" for user_id. It should be a foreign key, but not primary, as you want multiple records for same user.
    – Blauelf
    Oct 19, 2018 at 9:02

buy should not contain a session["user_id"] =, you would overwrite the user id of the current session that way. The only place for that is login. That might be what causes the error message, you assign None or some number to that variable, but probably not a valid user ID.

You don't use id in SQL statement, you should add user_id to the list of columns and add :id at the same position of the values.

You might want to pass numbers, not strings, for shares, but the type for the stock (the symbol) should be a TEXT or VARCHAR, unless you have another table translating those numbers to symbols.

balance does not belong in the portfolio, as cash is tracked at the user. And there's no input field of name "balance". You might want to store price and maybe transaction total for a nice history listing.

You must log in to answer this question.

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