i have tried so many options but to no avail pls below are the code i wrote.`@app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): """Buy shares of stock""" if request.method == "POST": symbol = request.form.get("symbol").upper() item = lookup(symbol)
if not symbol:
return apology("MISSING SYMBOL")
elif not item:
return apology("MISSING SYMBOL")
# Check share is numeric data type
try:
shares = int(request.form.get("shares"))
except:
return apology("INVALID SHARES")
# Check shares is positive number
if not (shares >= 0):
return apology("INVALID SHARES")
user_id = session["user_id"]
cash = db.execute("SELECT * FROM users WHERE id = ?",user_id)[0]["cash"]
item_name = item["name"]
item_price = item["price"]
total_price = item_price * shares
if cash < total_price:
return apology("not enough cash")
db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_prices, user_id)
db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_prices, user_id)
db.execute("INSERT INTO transactions(user_id, name, symbol, shares, price, type) VALUES(?, ?, ?, ?, ?, ?,)",user_id, item_name, shares, item_price, 'buy',symbol)
return redirect('/')
else:
return render_template("buy.html")`