title: Troubleshooting HTML and PHP / MySQL
body: <p>Long time reader, first time poster. I am a novice PHP enthusiast, and I have a page that I have been working. Right now I have the DB connection working well and my SELECT statement is giving me the info needed. My problems are two fold (maybe more after this post; <em>set your phasers to cringe</em>):</p>
  
  <ol>
  <li><p>At one point, I had the INSERT working, but it suddenly stopped and no amount of tweaking seems to bring it back. I have verified that the INSERT statement works in a seperate PHP file without variables.</p></li>
  <li><p>When I did have the INSERT working, every refresh of the page would duplicate the last entry. I have tried tried several ways to clear out the $_POST array, but I think some of my experimenting lead back to problem #1.</p></li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php 
  $dbhost = "REDACTED";
  $dbuser = "REDACTED";
  $dbpass = "REDACTED";
  $dbname = "guest_list";
  // Create a database connection
  $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  // Test if connection succeeded
  if(mysqli_connect_errno()) {
    die("DB's not here, man: " . 
        mysqli_connect_error() . 
        " (" . mysqli_connect_errno() . ")"
       );
  }
  // replacement for mysql_real_escape_string()
  function html_escape($html_escape) {
    $html_escape =  htmlspecialchars($html_escape, ENT_QUOTES | ENT_HTML5, 'UTF-8');
    return $html_escape;
  }
  
  // Posting new data into the DB
  if (isset($_POST['submit'])) {
    $first = html_escape($_POST['first']);
    $last = html_escape($_POST['last']);
    $contact = html_escape($_POST['contact']);
    $associate = html_escape($_POST['associate']);
  
    $insert = "INSERT INTO g_list (";
    $insert .= "g_fname, g_lname, g_phone, g_association) ";
    $insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
    $insert .= "LIMIT 1";
    $i_result = mysqli_query($connection, $insert);
  // I have verified that the above works by setting the varialble 
  // in the VALUES area to strings and seeing it update
  }
  
  $query  = "SELECT * ";
  $query .= "FROM g_list ";
  $query .= "ORDER BY g_id DESC";
  $q_result = mysqli_query($connection, $query);
  
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;title&gt;Guest List&lt;/title&gt;
      &lt;link href="guest.css" media="all" rel="stylesheet" type="text/css" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;header&gt;
        &lt;h1&gt;REDACTED&lt;/h1&gt;
        &lt;h2&gt;Guest Registry&lt;/h2&gt;
      &lt;/header&gt;
      &lt;div class="container"&gt;
        &lt;div class="registry"&gt;
          &lt;form name="formup" id="main_form" method="post"&gt;
            &lt;fieldset&gt;
              &lt;legend&gt;Please enter your name into the registry&lt;/legend&gt;
              &lt;p class="first"&gt;First Name: 
                &lt;input type="text" name="first" value="" placeholder="One or more first names" size="64"&gt;&lt;/p&gt;
              &lt;p class="last"&gt;Last Name:
                &lt;input type="text" name="last" value="" placeholder="Last name" size="64"&gt;&lt;/p&gt;
              &lt;p class="contact"&gt;Phone Number or Email:
                &lt;input type="text" name="contact" value="" placeholder="" size="32"&gt;&lt;/p&gt;
              &lt;p class="associate"&gt;Your relation?
                &lt;input type="text" name="associate" value="" placeholder="" size="128"&gt;&lt;/p&gt;
              &lt;p class="submit"&gt;
                &lt;input type="submit" name="submit" title="add" value="submit" placeholder=""&gt;&lt;/p&gt;
            &lt;/fieldset&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;h3&gt;Guest List:&lt;/h3&gt;            
      &lt;table&gt;
        &lt;tr&gt;
          &lt;th&gt;Firstname(s)&lt;/th&gt;&lt;th&gt;Lastname&lt;/th&gt;
          &lt;th&gt;Phone or Email&lt;/th&gt;&lt;th&gt;Association&lt;/th&gt;
        &lt;/tr&gt;
  
        &lt;?php while($guest = mysqli_fetch_assoc($q_result)) {
    echo "&lt;tr&gt;" . "&lt;td&gt;" . $guest["g_fname"] . "&lt;/td&gt;"
      . "&lt;td&gt;" . $guest["g_lname"] . "&lt;/td&gt;"
      . "&lt;td&gt;" . $guest["g_phone"] . "&lt;/td&gt;"
      . "&lt;td&gt;" . $guest["g_association"] . "&lt;/td&gt;" . "&lt;/tr&gt;";
  } ?&gt;
  
      &lt;/table&gt;
      &lt;footer&gt;
        &lt;div&gt;Copyright &lt;?php echo date("Y"); ?&gt;, REDACTED, LLC.&lt;/div&gt;
  
        &lt;?php
  if (isset($connection)) {
    mysqli_close($connection);
  }
        ?&gt;
      &lt;/footer&gt;
    &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>