0

I am trying to register a user. I am having problem in preventing the user to register if the username typed already exists in the database.

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""

    if request.method == "GET":
        return render_template("register.html")

    elif request.method == "POST":

        # Ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username", 400)

        # Ensure password was submitted
        elif not request.form.get("password"):
            return apology("must provide password", 400)

        # Ensure password confirmation was submitted
        elif not request.form.get("password-confirmation"):
            return apology("must provide password confirmation", 400)

        # Check if the password confirmation field matches the password field
        elif request.form.get("password-confirmation") != request.form.get("password"):
            return apology("Your passwords didn't match", 400)

        # Query database for that username
        row = db.execute("SELECT * FROM users WHERE username = ?", request.form.get("username"))

        # Database
        database = db.execute("SELECT * FROM users")

        # Ensure username is not already in the database
        if row in database:
            return apology("username already exists", 400)

        # Insert username and password into the database AND Hash user's password
        insert = db.execute("INSERT INTO users (username,hash) VALUES(?,?)" , request.form.get("username"), generate_password_hash(request.form.get("password")))

        # Remember which user has logged in
        session["user_id"] = insert

        # Redirect user to home page
        return redirect("/")

Instead of getting "username already exists", I get "internal server error".

Can someone please specify why it might be the case.?

Khan
  • 1
  • 1
  • `db.execute` likely requires an iterable containing the username to search for, not just the username itself, as an argument. `row = db.execute("SELECT * FROM users WHERE username = ?", (request.form.get("username"),))` – chepner Jul 25 '21 at 18:11
  • Can you please elaborate a bit.? – Khan Jul 25 '21 at 19:22
  • Hello @chepner, Can you please elaborate a bit on your above comment.? Awaiting your kind response – Khan Jul 26 '21 at 11:37
  • Read the documentation for whatever database library you are using. – chepner Jul 26 '21 at 12:41

2 Answers2

0

Your SELECT syntax is wrong. It should be rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))

haidousm
  • 556
  • 6
  • 21
0

This if row in database: will never be true.

row is a list, so it is trying to find a list in database. database is itself a list, but does not contain any lists, only dictionaries.

Program would know if username is already in users if row is not the empty list. Hint, Hint.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15