I'm trying to make it so when the user doesn't already own the stock, it adds it to the table. If they do already own it, it should update. The problem is when I try it, it buys 2x what it is supposed to, meaning both cases are working when only one should. I'm not sure what is causing the other one to run. This is my code
db.execute("SELECT user_id, stock FROM portfolio WHERE EXISTS UPDATE portfolio SET shares = shares + :newshares WHERE user_id = user_id AND stock = stock\
", user_id=session["user_id"], stock=stock["symbol"], shares = shares, newshares = newshares")
db.execute("SELECT user_id, stock FROM portfolio WHERE NOT EXISTS INSERT INTO portfolio (user_id, stock, shares) VALUES (:user_id, :stock, :shares) \
ON DUPLICATE KEY UPDATE shares = shares + :newshares", user_id=session["user_id"], stock=stock["symbol"], shares = shares, newshares = newshares)