My buy function was working when I tested it, buy yesterday, when I was testing my implementation of "index" it stopped. The "INSERT INTO portfolio" line is no longer executing properly, and I cannot figure out why. I attached my code for buy and the server log. Thank you for your help!
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
from time import gmtime, strftime
if request.method == "POST":
shares = request.form.get("shares")
symbol = request.form.get("symbol")
if not shares:
return apology("PROVIDE NUMBER OF SHARES")
if not symbol:
return apology("PROVIDE SYMBOL")
dictionary = lookup(symbol)
if not dictionary:
return apology("PROVIDE VALID SYMBOL")
price = dictionary['price']
symbol = dictionary['symbol']
cash = db.execute("SELECT cash FROM users WHERE id = :userid", userid=session["user_id"])
user = db.execute("SELECT username FROM users WHERE id = :userid", userid=session["user_id"])
time=strftime("%Y-%m-%d %H:%M:%S", gmtime())
cash = cash[0]["cash"]
shares = int(shares)
total = 0
for i in range(shares):
total += price
if cash >= price:
db.execute("INSERT INTO portfolio (User, Price, Time, Symbol, Shares, Total) VALUES (:userid, :price, :time, :symbol, :shares, :total)", userid=session["user_id"], price=price, time=time, symbol=symbol, shares=shares, total=total)
db.execute("UPDATE users SET cash = cash - :price WHERE id = :id", price=price, id=session["user_id"])
else:
return apology("NOT ENOUGH CASH")
return redirect ("/")
else:
return render_template("buy.html")