Implementing the Buy route and running into difficulty with updating the DB...I've tested the rest of my code which all works, but whenever I use the two db.execute lines at the end of the code, I get error 500... I've tried commenting out each individually as well to see where the problem stem from, but seems that both give a problem
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":
# check if stock exists, share input is valid
stock = lookup(request.form.get("symbol"))
try:
shares = int(request.form.get("shares"))
except:
return apology("shares must be a number")
if not stock:
return apology("Stock not found")
if shares < 1:
return apology("Sorry, can't buy less than one share")
# check if user has the $$
rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
funds = rows[0]["cash"]
price = stock["price"]
tx_value = shares * price
if tx_value > funds:
return apology("Sorry, insufficient cash to perform this transaction")
# insert tx into transactions and update user cash
db.execute("UPDATE users SET cash = cash - :tx_value WHERE id = user_id", tx_value=tx_value, user_id=session["user_id"])
db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES(:user_id, :symbol, :shares, :price)", user_id=session["user_id"], symbol=request.form.get("symbol"), shares=shares, price=price)**
return redirect("/")
else:
return render_template("buy.html")