I've looked at lots of other questions with people who have code for register, and my SQL code is exactly the same as most of them, but when I register, it lets multiple people have the same username. I don't know why this is, but really need some help.
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
session.clear()
if request.method == "POST":
if not request.form.get("username"):
return apology("must provide username!")
elif not request.form.get("password"):
return apology("must provide password")
elif not request.form.get("password_again"):
return apology("must provide password confirmation")
elif request.form.get("password") != request.form.get("password_again"):
return apology("Sorry, passwords didn't match. Try again.")
hash = generate_password_hash(request.form.get("password"), method='pbkdf2:sha256', salt_length=8)
result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username = request.form.get("username"), hash = hash)
if not result:
return apology("That username has already been taken.")
rows = db.execute("SELECT * FROM users WHERE username = :username", username = request.form.get("username"))
session["user_id"] = rows[0]["id"]
db.execute("INSERT INTO stocks (user_id) VALUES (:user_id)", user_id=rows[0]["id"])
return redirect("/")
else:
return render_template("register.html")