title: PHP Loop with dropdown selection and submit button?
body: <p>I am trying to make a loop that gets the name some other info about a product from a sql table - <a href="http://i.stack.imgur.com/ODs9I.jpg" rel="nofollow">MySQL table </a></p>
  
  <p>Then Creates a page that looks like that - <a href="http://i.stack.imgur.com/iZj1F.jpg" rel="nofollow">Webpage</a></p>
  
  <p>So Far I have this code that does show it but I cant figure out a way how to update the name of the dropdown menu so when I press submit It writes into another SQL table the name of the product and then how many of those products did the customer selected . </p>
  
  <pre><code>&lt;?php
  
  $sql = "SELECT * FROM product";
  $result = $conn-&gt;query($sql);
  while ( $row = mysqli_fetch_assoc($result) ) {
   $columnValues[] = $row['ProductID'];           
  foreach($columnValues as $key =&gt; $value) {
  $$key = $value;
  while ($row = $result-&gt;fetch_assoc()) {
  echo "&lt;tr&gt;\n". "&lt;br&gt;";
  echo "##product-ID## ";
  echo "&lt;td&gt;".$row['ProductID']. "&lt;/td&gt;\n";                           
  echo "  ##product-name## ";
  echo "&lt;td&gt;".$row['ProductName']."&lt;/td&gt;\n";                          
  echo "&lt;td&gt;\n";                              
  echo "  ##dropdown## ";
  echo "&lt;select id=$value&gt;\n";
  echo "&lt;option value='1'&gt;1&lt;/option&gt;\n";
  echo "&lt;option value='2'&gt;2&lt;/option&gt;\n";
  echo "&lt;option value='3'&gt;3&lt;/option&gt;\n";
  echo "&lt;option value='4'&gt;4&lt;/option&gt;\n";
  echo "&lt;option value='5'&gt;5&lt;/option&gt;\n";
  echo "&lt;option value='6'&gt;6&lt;/option&gt;\n";
  echo "&lt;/select&gt;\n";
  echo "&lt;/td&gt;\n";     
  echo "&lt;/tr&gt;\n";
   } 
      } 
          }
  
          $conn-&gt;close();                         
  ?&gt;
  &lt;html&gt;
   &lt;body&gt;
  &lt;form method="POST" action="#" &gt;
  &lt;input type="submit" name="Submit" value="Submit" /&gt;&lt;br&gt;
  &lt;/form&gt;                                             &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I know I will most likely need a second php script for the capture of the post so help with that will be greatly appreciated too . </p>
  
  <p>////////////////////////////////////////////////</p>
  
  <p>So up to here I got it somehow - It loops and shows all the product . When I press submit it adds only the last product in the loop and it doesn't care of the drop down menu - Just adds a "2" .</p>
  
  <p>The table where the script writes is simple - 4 columns OrderID1,productid1,ProductName1 orderedqnt1</p>
  
  <p>Thanks in advance . </p>
  
  <h2>Index.php</h2>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
    &lt;form method="POST" action="insert.php" &gt;
  &lt;?php
  
  session_start(); // session start for Variables to add to the sql in Insert.php
  include("global.php"); // Stores the session Variables
  
  ## Conection part 
  
  $sql = "SELECT * FROM product";
  $result = $conn-&gt;query($sql);
  
  while ( $row = mysqli_fetch_assoc($result) ) {
  
    $columnValues[] = $row['ProductID'];
    foreach($columnValues as $key =&gt; $value) {
      $$key = $value;
  
  while ($row = $result-&gt;fetch_assoc()) {
    echo "&lt;tr&gt;\n". "&lt;br&gt;";
    echo "##product-ID## ";
    echo "&lt;td&gt;".$row['ProductID']. "&lt;/td&gt;\n";
    echo "  ##product-name## ";
    echo "&lt;td&gt;".$row['ProductName']."&lt;/td&gt;\n";
    echo "&lt;td&gt;\n";
    /* echo "  ##dropdown## "; */
  
    echo "&lt;select id=" . $value . " name='dropdown'&gt;\n";  
  
    echo "&lt;option value=''&gt;-&lt;/option&gt;\n";
    echo "&lt;option value='1'&gt;1&lt;/option&gt;\n";
    echo "&lt;option value='2'&gt;2&lt;/option&gt;\n";
    echo "&lt;option value='3'&gt;3&lt;/option&gt;\n";
    echo "&lt;option value='4'&gt;4&lt;/option&gt;\n";
    echo "&lt;option value='5'&gt;5&lt;/option&gt;\n";
    echo "&lt;option value='6'&gt;6&lt;/option&gt;\n";
    echo "&lt;/select&gt;\n";
    /* echo "&lt;/td&gt;\n"; */
    /* Echo ":::::value variable = "."$value"; */
    echo "&lt;/tr&gt;\n" . "&lt;br&gt;";
  
  print_r ($value);
  
  
    $_SESSION['GrabIDses']=mysqli_real_escape_string($conn,$row['ProductID']);  //Grabs the ID of the product in Session Variable
    $_SESSION['GrabNameses']=mysqli_real_escape_string($conn,$row['ProductName']); //Grabs the Name of the product in Session Variable
    $_SESSION['GrabSKUses']=mysqli_real_escape_string($conn,$row['SKU']); //Grabs the SKU of the product in Session Variable
    $_SESSION['Ordered']=mysqli_real_escape_string($conn,$value); //Grabs the Ordered Quantity for  the product in Session Variable ????????????????
  
    /* $GrabID = mysqli_real_escape_string($conn,$row['ProductID']);
    $GrabName = mysqli_real_escape_string($conn,$row['ProductName']);
    $GrabSKU = mysqli_real_escape_string($conn,$row['SKU']);
    echo "----------------------"."$_SESSION['GrabSKUses']"."&lt;br&gt;"."$_SESSION['GrabIDses']"."&lt;br&gt;"."----------------------"; */
  
      }
    }
  }
  $conn-&gt;close();
  echo "&lt;br&gt;";
  ?&gt;
  
  &lt;input type="submit" name="Submit" value="Submit" /&gt;&lt;br&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <hr>
  
  <h2>Insert.php</h2>
  
  <pre><code>  &lt;?php
  session_start(); // session start
  
  $getvalue = $_SESSION['GrabIDses']; // session get
  $getvalue1 = $_SESSION['GrabNameses']; // session get
  $getvalue2 = $_SESSION['GrabSKUses']; // session get
  $ordered11 = $_SESSION['Ordered']; // session get
  
  echo $getvalue;
  echo "||";
  echo $getvalue1; 
  echo "||";
  echo $getvalue2;
  echo "||"."&lt;br&gt;";
  print_r($_SESSION);
  
  ## Connection Part
     if(isset($_POST['dropdown'])) {
  
          echo("You order was completed" . "&lt;br&gt;");           
          $sql = "INSERT INTO testorder (productid1,ProductName1,orderedqnt1) VALUES ('$getvalue', '$getvalue1','$ordered11')";
  
  
  
          if (mysqli_query($conn, $sql)) 
              { echo "New record created successfully"; }     
  
          else 
              { echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($conn); }
          mysqli_close($conn);
      }
  
  else {
  echo" dhur";
  }
  ?&gt;
  </code></pre>