title: Wrong query for table loop?
body: <p>I just want to ask your help about my table query, everytime when button is click, a new table will display. Instead of only a row will update to a table, an entire table display behind is the old table. Please help me review my code.Thanks</p>
  
  <p>demo.insert.php</p>
  
  <pre><code>$dbHost = 'localhost'; // usually localhost
  $dbUsername = 'root';
  $dbPassword = 'thartpc';
  $dbDatabase = 'hospital';
  $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to      connect to Database Server.");
  mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
  
  $sql_check = mysql_query("SELECT * FROM messages order by med_id desc");
  
  if(isSet($_POST['content2'])){
      $content2=$_POST['content2'];
      $content3=$_POST['content3'];
      mysql_query("insert into messages(medname, quantity) values    ('$content2','$content3')");
      $sql_in= mysql_query("SELECT * FROM messages order by med_id desc");
      $r=mysql_fetch_array($sql_in);
  }
  
  ?&gt;
  
  &lt;?php
  
  require_once 'config.php';
  $query = mysql_query("SELECT * FROM messages order by med_id desc") or die   ("could not search");
  $count = mysql_num_rows($query);   
  
  ?&gt;
  &lt;div class="container"&gt;
      &lt;div class="panel panel-info"&gt;
          &lt;div class="panel-heading" class="panel-title"&gt;
              &lt;h7&gt;Name&lt;/h7&gt;
          &lt;/div&gt;
          &lt;div class="panel-body" style="padding-top:30px"&gt;
  &lt;?php
  echo "
              &lt;table class='table table-hover';&gt;
                  &lt;tr&gt;
                      &lt;td style='border:1px solid; color:gray' align='center'&gt; ID&lt;/td&gt;
                      &lt;td style='border:1px solid; color:gray' align='center'&gt;Name&lt;/td&gt;
                      &lt;td style='border:1px solid; color:gray' align='center'&gt;Quantity&lt;/td&gt;
                  &lt;/tr&gt;";
  while($row = mysql_fetch_array($query)){
      echo "
                  &lt;tr&gt;
                      &lt;td style='border:1px solid; color:gray' align='center'&gt;".$row['med_id']."     &lt;/td&gt;
                      &lt;td style='border:1px solid; color:gray'  align='center'&gt;".$row['medname']." &lt;/td&gt;
                      &lt;td style='border:1px solid; color:gray'  align='center'&gt;".$row['quantity']."&lt;/td&gt;
                  &lt;/tr&gt; ";
  }
  echo "      &lt;/table&gt;";  
  
  ?&gt;    
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>inserting.php</p>
  
  <pre><code>&lt;?php
  session_start();
  
  if(isset($_POST['search'])){
      $globalpid = $_POST['search'];
      $_SESSION['search'] = "$globalpid";
  }
  
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
  
          &lt;link rel="stylesheet"    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"&gt;
          &lt;link rel="stylesheet" type="text/css" href="css/custom.css"&gt;
          &lt;!-- Optional theme --&gt;
          &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap- theme.min.css"&gt;
          &lt;title&gt;Insert Record with jQuery and Ajax&lt;/title&gt;
          &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"&gt;&lt;/script&gt;
  
          &lt;script type="text/javascript"&gt;
          $(function(){
              $("#v").click(function(){
                  var element = $(this);
                  var test2 = $("#content2").val();
                  var test3 = $("#content3").val();
                  var dataString = 'content2='+ test2+'&amp;content3='+ test3;
                  if(test2==''){
                      alert("Please Enter Some Text");
                  }else{
                      $("#flash").show();
                      $("#flash").fadeIn(400).html('&lt;img src="http://tiggin.com/ajax-loader.gif"       align="absmiddle"&gt;&amp;nbsp;&lt;span class="loading"&gt;Loading Comment...&lt;/span&gt;');
                      $.ajax({
                          type: "POST",
                          url: "demo_insert.php",
                          data: dataString,
                          cache: false,
                          success: function(html){
                              $("#display").after(html);
                              document.getElementById('content2').value='';
                              $("#flash").hide();
                          }
                      });
                  }
                  return false;
              });
          });
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;?php
          require_once 'pharmacyheader.php';
          require_once 'pharmacysearch.php';
          require_once 'patientquery.php';
      ?&gt;
          &lt;div class="container"&gt;
              &lt;form  method="post" name="form" action=""&gt;
                  &lt;div class="col-md-6"&gt;
                      &lt;div class="panel panel-info"&gt;
                          &lt;div class="panel-heading"&gt;&lt;h7&gt;Name A - F&lt;/h7&gt;&lt;/div&gt;
                          &lt;div class="panel-body"&gt;
                              &lt;select class="form-control" id="content2" name="content2"&gt;
                                  &lt;option id="0" style="width:100px"&gt;&lt;/option&gt;
      &lt;?php
          require_once 'config.php';
          $medicine = mysql_query("SELECT * FROM medicine");
          while ($row = mysql_fetch_array($medicine)) {
              echo '&lt;option id="' . $row['medicinename']  . '"';
              echo ' value="' . $row['medicinename'] . '"';
              if($row['brandid'] == $row['medicinename']) {
                  echo ' selected="selected"'; 
              }
              echo '&gt;';
              echo $row['medicinename'];
              echo '&lt;/option&gt;';               
          }
      ?&gt;
                              &lt;/select&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;                      
                  &lt;div class="col-md-6"&gt;
                      &lt;div class="panel panel-info"&gt;
                          &lt;div class="panel-heading"&gt;&lt;h7&gt;Quantity&lt;/h7&gt;&lt;/div&gt;
                          &lt;div class="panel-body"&gt;
                              &lt;select class="form-control" id="content3" name="content3"&gt;
                                  &lt;option id="0" style="width:100px"&gt;&lt;/option&gt;
      &lt;?php
      require_once 'config.php';
      $quantity = mysql_query("SELECT * FROM quantity");
      while ($row = mysql_fetch_array($quantity)) {
          echo '&lt;option id="' . $row['quantityname']  . '"';
          echo ' value="' . $row['quantityname'] . '"';
          if($row['quantityid'] == $row['quantityname']) {
              echo ' selected="selected"'; 
          }
          echo '&gt;';
          echo $row['quantityname'];
          echo '&lt;/option&gt;';               
      }
      ?&gt;                                  
                              &lt;/select&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row"&gt;&lt;br /&gt;
                      &lt;div class="col-md-1"&gt;
                          &lt;input type="submit" value="Add"  id="v" name="submit" class="form-control"      /&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;div style="height:7px"&gt;&lt;/div&gt;
          &lt;div id="flash" align="left"  &gt;&lt;/div&gt;
          &lt;div id="display" align="left"&gt;&lt;/div&gt;
          &lt;script     src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script   src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"&gt;       &lt;/script&gt;
          &lt;script src="typeahead.min.js"&gt;&lt;/script&gt;
          &lt;script src="http://code.jquery.com/jquery-2.1.1.min.js"&gt;&lt;/script&gt;
          &lt;script   src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"&gt;         &lt;/script&gt;
          &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
          &lt;script src="js/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript" src="jquery.min.js"&gt;&lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>