query mysql and pass array to a form to create dynamic drop down list in PHP
I am new to PHP mysql progamming, so please excuse me if I am doing something silly.
My objective is to create a form that presents a drop down list, which is dynamically populated based on a query from a mysql database. I have the main controller (sell.php), which should query the database and then should pass an array to the form called sell_form.php.
The form seems to be working ok when I define the array within the form manually or even within the controller manually. So, it seems that somehow when I am querying the database and trying to build the array, I am doing something wrong.
Hopefully, someone can point me to the right direction.
content of sell_form.php :
<form action="sell.php" method="post">
<?php
// $stocksym = array(5,6,7);
?>
<fieldset>
<div class="control-group">
<?php
echo '<select>';
foreach ($stocksym as $key => $value)
{
echo "<option value=$key> $value </option>";
}
echo '</select>';
?>
</div>
<div class="control-group">
<button type="submit" class="btn">Sell</button>
</div>
</fieldset>
</form>
The relevant content of sell.php (this is where I beleive I am doing something wrong):
$rows = query("SELECT * FROM portfolio WHERE id = ?", $_SESSION["id"]);
foreach ($rows as $row))
{
$stocksym = $row["symbol"];
}
The relevant mysql table is:
mysql> select * from portfolio;
+----+--------+--------+
| id | symbol | shares |
+----+--------+--------+
| 1 | AAPL | 2 |
| 1 | MSFT | 15 |
+----+--------+--------+
2 rows in set (0.00 sec)
Subject
Written By
Posted
query mysql and pass array to a form to create dynamic drop down list in PHP
January 22, 2013 08:40AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.