0

I am trying to get the code to work and am not worrying about sql injections yet.

Code from Buy function

 current_cash = db.execute("SELECT cash FROM users WHERE  id=current_id")

I asked a similar question here and did not get an answer. The post below contains code from login function.

https://www.reddit.com/r/cs50/comments/bm9rq7/does_sessionuser_id_carry_over_from_the_login_and/

The main error in the image below is caused by

session["user_id"] = username_list_check[0]["id"] 

in the register function. Why is that?

Below contains the error message

enter image description here

Below contains register function in application.py Could you tell me what is wrong with register function?

enter image description here

Just for clarity I will add login, not that I think I changed anything but you never know.

enter image description here

Do I need to post the entire code from the buy function?

To summarize my questions, why doesn't current_username work? Does my psuedocode in the example from reddit work in the link?

Thanks for the help.

2
  • This indicates a problem in register. Please edit register function into the question. – DinoCoderSaurus May 12 '19 at 11:40
  • @DinoCoderSaurus I added the register and login function code. Can you tell me the error in register function? – grggthwrth May 13 '19 at 3:28
0

Please do not post images of code. One should post code as text, then format it properly. One way to format is to paste the code, select the code, click {} in the format bar.

This call is tagged pset7. In the current edX version of the course, finance is pset8. If you are pursuing a grade/verified certificate be sure to check the course homepage for the current courseware.

Now, down to business. The fatal flaw in this code is the SELECT from users table. According to the Hints in the spec:

If str is a SELECT, then execute returns a list of zero or more dict objects, inside of which are keys and values representing a table’s fields and cells, respectively.

If a new user is registering, presumably there is no matching record in the users table, so it returns zero dict objects and therefore gives the invalid index message here session["user_id"] = username_list_check[0]["id"].

But is that statement necessary? Review the walkthrough for register, it gives a very good description of the flow. Essentially

  • validate input from register form (return apology if necessary)
  • perform an INSERT query and test the result (around 2:18 in the walkthrough)

The info you need to test the result is also found in the Hints section:

  • If str is an INSERT, and the table into which data was inserted contains an autoincrementing PRIMARY KEY, then execute returns the value of the newly inserted row’s primary key.

......

If an INSERT or UPDATE would violate some constraint (e.g., a UNIQUE index), then execute returns None. In cases of error, execute raises a RuntimeError.

Once you have a working register, then it's time to move onto the next function.

2
  • Before I click the check-mark is okay if I post the error message as a picture? – grggthwrth May 14 '19 at 0:00
  • It is preferable to post the error message as text. It makes it easier to copy/paste into the answer if necessary. And the pictures are teeny-tiny, difficult for old eyes to read :) – DinoCoderSaurus May 14 '19 at 0:11

You must log in to answer this question.

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