My buy function was working (I thought), but after I implement3d index, I have realized that it is only allowing each registered member to buy one stock. After that, it appears to work (showing no error messages), but there are no other stocks added to the index or to my phpLite tables. I have no idea why this is happening. If someone could tell my where my logic/code is wrong that would really be amazing! Thanks in advance!!!

My buy code:

@app.route("/buy", methods=["GET", "POST"])
def buy():
"""Buy shares of stock."""
if request.method == "POST":
    #lookup stock
    stock = lookup(request.form.get("symbol"))

    #check if stock is a real thing
    if not stock:
        return apology("the stock you entered does not exist")

    #check to make sure positive number was entered
        number = int(request.form.get("number"))
        if number <= 0:
            return apology("please enter a positive number")
        return apology("please enter a positive NUMBER:(")

    #check current amount of money
    cash = db.execute("SELECT cash FROM users WHERE id= :id", \

    #check if they have the money
    if not cash or float(cash[0]["cash"])< stock["price"] * number:
        return apology("you do not have enough money")

    #update in history table
    db.execute("INSERT INTO history (symbol, shares, price, id) \
            VALUES(:symbol, :shares, :price, :id)", \
            symbol= request.form.get("symbol"), \
            shares= number, \
            price= usd(stock["price"]), \
            id = session["user_id"])

    #update user cash
    db.execute("UPDATE users SET cash = cash - :order WHERE id = :id", \
            id=session["user_id"], \
            order=stock["price"] * float(number))

    # select user shares of symbol
    user_shares = db.execute("SELECT shares FROM portfolio \
            WHERE id = :id AND symbol = :symbol", \
            id = session["user_id"], \
            symbol = stock["symbol"])

    #if user doesn't already have that stock --> create a new object
    if not user_shares:
        db.execute("INSERT INTO portfolio (name, shares, price, total, symbol, id) \
                    VALUES(:name, :shares, :price, :total, :symbol, :id)", \
                    name=stock["name"], \
                    shares= number,
                    price= usd(stock["price"]), \
                    total=  usd(stock["price"]* number), \
                    symbol= stock["symbol"],
                    id= session["user_id"])

    #if user already has it --> increase number of shares
        shares_total = int(user_shares[0]["shares"]) + number
        db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, \
                    id=session["user_id"], \
                    symbol = stock["symbol"])

    return redirect(url_for("index"))

    return render_template("buy.html") 
  • Are there primary keys on history and portfolio? If so, how are they defined? Commented Jul 24, 2017 at 19:21

1 Answer 1


This is usually a problem in the database and not the code. If the portfolio table has a unique or primary key or index on the userid, it will only allow one entry for any given userid. Additionally, if there is a separate unique key or index on the symbol, it would only allow one entry for that stock in the table, no matter who buys it. If the goal is to only have one entry or row for the combination of the user and the stock symbol, then a unique index needs to be created for the combination of the user id and symbol. It is possible to create a unique index for the combination of multiple columns.

BTW, remember that after the unique key is created across two columns, if an attempt is made to add a second row with the same two column values, it will fail. In this case "ON DUPLICATE" is your friend. ;-)

Also, although I haven't done this pset this year yet, I don't think I see anything in the code that handles a returned error. If that's the case, I don't think the error would display. I'll leave it to someone with python experience to respond to this. (I could be wrong on this.)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • How did you manage to solve this issue because I have the same problem
    – aash123
    Commented Dec 7, 2017 at 14:32

You must log in to answer this question.

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