0

Like it says on the tin: all of a sudden I can only buy stocks of a single company in pset7. For example, I logged in as John Harvard, and bough 10 shares of Google. I then tried to buy 10 shares of a different company, but instead I had another 10 shares of Google added to my portfolio.

I find this strange, because when I var_dump-ed the variables that I am using in the INSERT statement, they all printed out just as they should be.

The (I hope) relevant snippet of my code is here:

$insert = CS50::query("INSERT INTO portfolios (users_id, symbol, shares) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)", $_SESSION["id"], $stock["symbol"], $_POST["shares"]);

I don't know if this is relevant, but I have implemented the joint key mentioned in the pset spec in my database (so users_id and symbol were together marked as unique).

I've been on this for some time, and tried implementing the above statement in a few different ways, so I'll appreciate help.

EDIT: this is what my 'portfolios' table looks like:

+----------+------------------+------+-----+---------+-------+
| Field    | Type             | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| id       | int(10) unsigned | NO   | PRI | NULL    |       |
| users_id | int(10) unsigned | NO   | MUL | NULL    |       |
| symbol   | varchar(255)     | NO   |     | NULL    |       |
| shares   | int(10) unsigned | NO   |     | 0       |       |
+----------+------------------+------+-----+---------+-------+
0
1

Solved this now (after a veeeery long day).

The problem was indeed with my database: I forgot to add auto-increment to id, so the database went a bit crazy when inserting.

0

$stock["symbol"] does not come from the $_POST superglobal variable. It's generated elsewhere by a call to lookup(). Look at that section of your code to see why it's returning GOOG every time.

EDIT: I edited my answer to say $_POST instead of $_GET, and lookup() instead of query(). But yeah ... if the value of $stock["symbol"] really is changing appropriately based on the user input submitted via $_POST, then I'm stumped.

Do me a favor. Open a new Terminal tab in the CS50 IDE, and execute the following commands:

mysql50 shell
USE `pset7`;
DESCRIBE `portfolios`;
exit

I have a hunch that you are triggering a duplicate purchase on the users_id field somehow, which is causing it to add the value of the shares to the only line in the portfolios table that matches that users_id.

3
  • Oh, no. I should have mentioned that in this case I simply get stock like so : $stock = lookup($_POST["symbol"]); so it is based on user input submitted via $_POST. And then when I check the value of $stock["symbol"] by printing it, it does change according to the stock (so it's not always GOOG, it's whatever the user wants). That's why I find it so strange that the code does not work. Do you think the problem could be located elsewhere? (I am avoiding pasting in all of buy.php to avoid honour code violations). – iCode95 Aug 21 '16 at 19:15
  • I edited my answer to say $_POST instead of $_GET, and lookup() instead of query(). But yeah ... if the value of $stock["symbol"] really is changing appropriately based on the user input submitted via $_POST, then I'm stumped. – hotwebmatter Aug 22 '16 at 2:18
  • So I followed your instructions -- see the edit to my original question for the results I got. Is there a problem with the databse then? I think I might have 'broken' something when creating the double-key or just playing around :P And thank you for your help so far! – iCode95 Aug 22 '16 at 12:58

You must log in to answer this question.

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