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 :
else:
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 :
else:
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!