I'm working on register as part of the finance pset, and I think I have just about all of it, but I don't know where I need to write the SQL, my understanding is that when they click the register button, it should add the info into the SQL table and check to make sure it's unique. I'm not sure how to do this, any help would be appreciated, thanks


Essentially expanding my previous answer. Please give some feedback on how I can provide better help.

Note that this year's finance specification includes tests and therefore requires specific values for the name attributes in the HTML form.

I basically do register in a few steps:

  1. Check that username and password are provided and password and confirmation match (if any of these are not as expected, apologise)
  2. First SQL query: select statement on users searching for a record with that same username (similar to the query in login, but without the password)

    If that query returns a non-empty list, the name is already taken. If the result of the query instead is None, everything is fine.

  3. If all went fine so far, now an insert statement, specifying the username and the hashed password for the corresponding fields. id and cash value are set up to be filled by the database.

Always do your SQL statements like db.execute("SELECT * FROM users WHERE name=:uname;", uname=username) instead of dangerous concatenation like db.execute("SELECT * FROM users WHERE name='"+username+"'"). Python functions when properly set up can take named parameters of form name=value, like in this case arbitrarily named parameter uname (here you can pick the name), and db.execute for example will insert those into the query in a safe way (like it will put quotes around a string value, and escape quotes that are within the string).

  • This is very helpful, but the part I'm really stuck on is where I need to write the sql so it adds/checks for a user after registering. Thanks – Joel Banks Jan 29 '18 at 1:25
  • What do you have? Checked the POST variables username, password, and confirmation? Do you have the SQL statement to check for an existing account with same username? Your question gives little context. Also, make sure you're familiar with your table layout, with the names and types of the fields. – Blauelf Jan 29 '18 at 8:47
  • I have the code that checks to make sure they aren't blank and that they match. I do not have any sql yet because I do not understand where I need to write it. That is the part I am stuck on, I do not understand where to write sql. Thanks – Joel Banks Jan 29 '18 at 22:35
  • For the first select statement, copy the select from login and remove the password/hash part, keeping the name comparison. If the result is non-empty, the name is in use. – Blauelf Jan 30 '18 at 5:52
  • So I guess my question is, where do I write the SQL code so that when I click register, it runs my SQL. I see the sql in the database but it doesn't look like it would run on a button press like register, which is what I need help with – Joel Banks Jan 30 '18 at 20:45

You must log in to answer this question.

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