0

I'm right on the cusp of finishing the pset7 after marathoning it today after not having touched CS50 in about a month. I have two final issues that can't seem to be resolved.

The first has to do with buy. I can't seem to get preg_match working correctly to check the user's input. This is how I have it right now:

else if(preg_match("/^\d+$/", $_POST["amount"]) === FALSE)
    {
        apologize("You must enter a whole, positive number into the second field.");
    }

It's part of a bigger set of checks, which is why it starts with an else if here. Is this setup right? It doesn't reject my inputs when I try, for example, 2.4.

Lastly, I have an issue with sell. I want to make sure the user cannot sell stocks he or she doesn't own, so I thought this would check that:

else if(query("SELECT symbol FROM stocks WHERE symbol = ? AND id = ?", $_POST["sell_symbol"], $_SESSION["id"]) === FALSE)
    {
        apologize("You must enter a symbol of a stock you own.");
    }

Well, simply put, it doesn't, and the transaction goes through no matter what causing an error on the other end.

What am I doing wrong with these two issues? I feel the answers are simple, but I haven't been able to figure it out yet.

Thanks in advance!

  • you know that the first condition that is evaluated to true in an if/else if/else sequence is the one whose body is executed and all the following ones are simply ignored, right? – Kareem Nov 28 '15 at 12:23
  • This is part of a large section of checking user input, so if it is ignored because another if else was executed, I don't mind. My problem is, I can't get it to execute when it should be the one in the set of if conditions to execute. – Chris Nov 28 '15 at 14:17
  • mind showing us the whose if/else if/else sequence? – Kareem Nov 28 '15 at 15:19
  • I just managed to fix both issues. The first one was rather easy to figure out in the end. The second one I just changed to else if(!query..., and that fixed it. – Chris Dec 5 '15 at 7:48
0

I just managed to fix both issues. The first one was rather easy to figure out in the end. The second one I just changed to else if(!query..., and that fixed it.

| improve this answer | |

You must log in to answer this question.

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