title: Using a MySQL table-populated PHP dropdown to insert selection into another table
body: <p>I'm making a web app to help determine whose turn it is to make tea in my office, giving me a focus to learn the use of PHP/MySQL. Apologies for newbie ignorance.</p>
  
  <p>For new users signing up I need to populate the user table with their selections from a dropdown, which is itself populated from a separate table. So when a user signs up, I wan them to select the name of their favourite drink from the dropdown/drinks table and I want the ID of that drink saved in the defaultdrink field of the user table. I also understand this should be done using POST, not GET.</p>
  
  <p>Have so far successfully made a form that populates the DB and have made a dropdown populated from the DB - but no success yet in doing both.</p>
  
  <p>The form page is...</p>
  
  <pre><code>   &lt;?php 
  require "insert_dropdown.php";
  ?&gt;
  
  &lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1"&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;form name="form1" method="post" action="insert_ac.php"&gt;
  &lt;table width="100%" border="0" cellspacing="1" cellpadding="3"&gt;
  &lt;tr&gt;
  &lt;td colspan="3"&gt;&lt;strong&gt;Sign up to the Tea App&lt;/strong&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td width="71"&gt;Name&lt;/td&gt;
  &lt;td width="6"&gt;:&lt;/td&gt;
  &lt;td width="301"&gt;&lt;input name="name" type="text" id="name"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  
  &lt;tr&gt;
  &lt;td colspan="3" align="center"&gt;&lt;input type="submit" name="Submit" value="Submit"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;?php
  $dropdown = "&lt;select name='drinkname'&gt;";
  while($row = mysql_fetch_assoc($dresult)) {
    $dropdown .= "\r\n&lt;option value='{$row['drinkname']}'&gt;{$row['drinkname']}&lt;/option&gt;";
  }
  $dropdown .= "\r\n&lt;/select&gt;";
  echo $dropdown;
  ?&gt;
  </code></pre>
  
  <p>The form actions are led by insert_ac.php...</p>
  
  <pre><code>&lt;?php
  
  $host=""; // Host name 
  $username=""; // Mysql username 
  $password=""; // Mysql password 
  $db_name="tea"; // Database name 
  $tbl_name="users"; // Table name
  
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // Get values from form 
  $name=$_POST['name'];
  $pref=$_POST['pref']; // Drink preference
  
  
  // Insert data into mysql 
  $sql="INSERT INTO $tbl_name(name, pref)VALUES('$name', '$pref')";
  $result=mysql_query($sql);
  
  // if successfully insert data into database, displays message "Successful". 
  if($result){
  echo "Successful";
  }
  
  else {
  echo "ERROR";
  }
  
  // close connection 
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>And I'm populating the dropdown using insert_dropdown.php...</p>
  
  <pre><code>&lt;?php
  
  $host=""; // Host name 
  $username=""; // Mysql username 
  $password=""; // Mysql password 
  $db_name="test"; // Database name 
  
  
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // Write out our query.
  $dquery = "SELECT drinkname FROM drinks";
  // Execute it, or return the error message if there's a problem.
  $dresult = mysql_query($dquery) or die(mysql_error());
  
  // if successfully insert data into database, displays message "Successful". 
  if($dresult){
  echo "Drink Successful";
  echo "&lt;BR /&gt;";
  }
  
  else {
  echo "ERROR";
  }
  
  // close connection 
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>Am I beyond saving?</p>
  
  <p>Cheers,</p>
  
  <p>Alex</p>