0

I was wondering how do I retrieve the value (shares * price) of the stock sold in sell.php in order to update cash in users. In the template sell_form.php, I have the following code

<select name="stock">   
<?php foreach ($selling as $sellin)
 {
    print"<option value=" .$sellin["symbol"].">".$sellin["symbol"]."</option>";
 }    
 ?>
</select>

In sell.php, the code

$upd = query("UPDATE users SET cash=cash+? WHERE id=?", $sellin["value"], $_SESSION["id"]);

doesn't work. I tried a couple of other options, none of them work. Any suggestions will be greatly appreciated.

Can we only access associative arrays through a foreach loop?

1
  • What do you mean "can we only access associative arrays through a foreach loop"?
    – kzidane
    Aug 31 '14 at 8:51
1

If you're submitting using POST as a method, then you should be able to access the currently selected item in the list from sell.php using something like

$_POST["stock"]

If you're submitting using GET as a method, then you should be able to access the currently selected item in the list from sell.php using something like

$_GET["stock"]

where "stock" is the value of the name attribute of the <select> element.

2
  • I understand that but in the form, I'm asking the user for stock symbols only.And I retrieved that symbol using $_POST["stock"] but now I need the value of the stock symbol they provided, i.e. shares * price. This information is not in the template. However, my selling array has "value" stored in it. When I use a foreach loop, I am able to update cash using the query I mentioned above. But that's obviously not the correct way to do it because that adds the cash of user's current stocks. When I try using $selling["value"], it doesn't work.
    – hunter
    Aug 31 '14 at 15:44
  • @hunter, you need to modify the values in the array by reference. Take a look at the foreach manual!
    – kzidane
    Aug 31 '14 at 16:06

You must log in to answer this question.

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