0

I use mysql for inserting data into dropdown menu. I got multiple dropdown menus - each dropdown menu has name id - this information i get from database($stmt). The content of each dropdown menu I get by another mysql query $stmt2. I need to determine which id was set in every dropdown menu. These values I need to use later. This is my solution, which doesn't work. It won't print out anything.

echo "<table class ='centered_div' border='1px solid black'>";
echo "<tr><td>ID</td><td>food</td><td>g</td></tr>";
while($rows = $stmt->fetch()){

    echo "<tr><td>" . $rows['id'] . "</td><td>".$rows['food'];
    echo "<form action='upravit_plan.php' method='post'><select name =".$rows['id']." > ";
    $stmt2 = $db->query($q2);
    echo "<option value ='nothing'> choose one </option>";
    while($rows2 = $stmt2->fetch())
    {
        echo '<option value = '.$rows2['id'].'>'.$rows2['food'].'</option>';
    }
    echo "</select></form>";
    echo "</td> <td>" . $rows['g'] . "g</td></tr><br> ";

};
echo "<tr><td colspan='3'><form action='upravit_plan.php' method='post'><input name = 'go' type='submit' value='OK'/></form></td></tr>";
echo "</table>";

if(isset($_POST['go']))
{ 
    $stmt = $db->query($q);
    while ($rows = $stmt->fetch())
    {

     echo $_POST[$rows['id']];  // trying to print values 

    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154
user7303261
  • 128
  • 12
  • I doubt that "it won't print out *anything*." You put each of your ` – miken32 Mar 30 '17 at 23:21
  • @miken32 If they would be in one form - named by one `id` - I would be able to recognize which value was set to dropdown menus. Not sure if you understand my problem. – user7303261 Mar 30 '17 at 23:32

1 Answers1

0

You need to have all elements in a single form. You should also try to separate your PHP from your HTML, this might be a good start but still needs things like HTML escaping. Note in particular the use of alternative syntax and short echo tags.

<form action='upravit_plan.php' method='post'>
<table class ='centered_div' border='1px solid black'>
  <tr>
    <th>ID</th>
    <th>food</th>
    <th>g</th>
  </tr>

<?php while($rows = $stmt->fetch()):?>
  <tr>
    <td><?=$rows['id']?></td>
    <td>
      <?=$rows['food']?>
      <select name="<?=$rows['id']?>">
        <option value ='nothing'> choose one </option>

<?php while($rows2 = $stmt2->fetch()):?>
        <option value="<?=$rows2['id']?>"><?=$rows2['food']?></option>

<?php endwhile;?>
      </select>
    </td>
    <td><?=$rows['g']?>g</td>
  </tr>

<?php endwhile;?>
  <tr>
    <td colspan='3'><input name='go' type='submit' value='OK'/></td>
  </tr>
</table>
</form>

<?php
if(isset($_POST['go']))
{ 
    $stmt = $db->query($q);
    while ($rows = $stmt->fetch())
    {

     echo $_POST[$rows['id']];  // trying to print values 

    }
}
?>
miken32
  • 42,008
  • 16
  • 111
  • 154