title: Populate an html drop down using php mysqli
body: <p>my overall aim is to manage groups of links on a web page. I have four form pages to manage them; </p>
  
  <ul>
  <li>one to enter the name of a new link group (section)</li>
  <li>one to list the sections</li>
  <li>one to enter the name of a new link, it's URL, and the section under which it should appear</li>
  <li>one to list the links, URLs, and section names</li>
  </ul>
  
  <p>For this I have two tables "links" and "sections" I want to retrieve the section names from the section table, display them in a drop-down on the new links page, and write them into the links table with the link name and URL. </p>
  
  <p>My problem at the moment is retrieving the section list as a drop-down. I've tried a few things but am very inexperienced so either they are nonsense or I've made small mistakes that trip it up, but I can't tell which.</p>
  
  <p>Here's what I have (that doesn't work);</p>
  
  <pre><code>&lt;?php
  
  
  include("connect-db.php");
  
  
  function renderForm($Link = '', $URL = '', $Sectionname = '', $error = '', $ID = '') {
      ;
      }
   ?&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"         "http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;New Link&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;New Link&lt;/h1&gt;
  &lt;?php if ($error != '') {
  echo "&lt;div style='padding:4px; border:1px solid red; color:red'&gt;" . $error
  . "&lt;/div&gt;";
  } ?&gt;
  
  &lt;form action="" method="post"&gt;
  &lt;div&gt;
  &lt;?php if ($ID != '') { ?&gt;
  &lt;input type="hidden" name="ID" value="&lt;?php echo $ID; ?&gt;" /&gt;
  &lt;p&gt;ID: &lt;?php echo $ID; ?&gt;&lt;/p&gt;
  &lt;?php } ?&gt;
  
  &lt;strong&gt;Link name: *&lt;/strong&gt; &lt;input type="text" name="Link"
  value="&lt;?php echo $Link; ?&gt;"/&gt;&lt;br/&gt;
  &lt;strong&gt;URL: *&lt;/strong&gt; &lt;input type="text" name="URL"
  value="&lt;?php echo $URL; ?&gt;"/&gt;
  
  
  &lt;?php
  
  }
  $query = 'SELECT Section FROM sections';
  
  $result = mysqli_query($mysqli, $query);
  if( ! $result ) {
  echo mysql_error();
  exit;
  }
  
  echo '&lt;select name="dropdown"&gt;';
  echo '&lt;option value="0"&gt;Select a section please&lt;/option&gt;';
  
  while ($row=mysqli_fetch_array($result)) {
  echo '&lt;option value="' . $row['Section'] . '"&gt;' . $row['Section'] .     '&lt;/option&gt;';
  }
  echo '&lt;/select&gt;';
  }
  ?&gt;
  
  &lt;p&gt;* required&lt;/p&gt;
  &lt;input type="submit" name="submit" value="Submit" /&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php }
  
  // if the form's submit button is clicked, we need to process the form
  if (isset($_POST['submit']))
  {
  // get the form data
  $Link = htmlentities($_POST['Link'], ENT_QUOTES);
  $URL = htmlentities($_POST['URL'], ENT_QUOTES);
  
  
  // check that Link and URLame are both not empty
  if ($Link == '' || $URL == '')
  {
  // if they are empty, show an error message and display the form
  $error = 'ERROR: Please fill in all required fields!';
  renderForm($Link, $URL, $Sectionname, $error);
  }
  else
  {
  // insert the new record into the database
  if ($stmt = $mysqli-&gt;prepare("INSERT links (Link, URL) VALUES (?, ?)"))
  {
  $stmt-&gt;bind_param("ss", $Link, $URL, $Sectionname);
  $stmt-&gt;execute();
  $stmt-&gt;close();
  }
  // show an error if the query has an error
  else
  {
  echo "ERROR: Could not prepare SQL statement.";
  }
  
  // redirect the user
  header("Location: linkview.php");
  }
  
  }
  // if the form hasn't been submitted yet, show the form
  else
  {
  renderForm();
  }
  }
  
  // close the mysqli connection
  $mysqli-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>Many thanks</p>
  
  <p>GD</p>
  
  <p>connect-db.php follows </p>
  
  <pre><code>&lt;?php
  
  // server info
          $servername = "localhost";
          $username = "Admin";
          $password = "Admin";
          $dbname = "dashboard2";
  
  // connect to the database
  $mysqli = new mysqli($servername, $username, $password, $dbname);
  
  // show errors (remove this line if on a live site)
  mysqli_report(MYSQLI_REPORT_ERROR);
  
  ?&gt;
  </code></pre>