title: HTML form input for MySQL
body: <p>I have a MySQL table named as letter and wish to insert records through the  HTML data input form. Code as follows:</p>
  
  <pre><code>&lt;head&gt;
  &lt;meta charset="utf-8"&gt;
      &lt;link href="css/jquery-ui-1.10.1.css" rel="stylesheet"&gt;
      &lt;script src="js/jquery-1.9.1.js"&gt;&lt;/script&gt;
      &lt;script src="js/jquery-ui-1.10.1.min.js"&gt;&lt;/script&gt;
      &lt;script&gt;
         $(function() {
            $( "#datepicker" ).datepicker(({ dateFormat: "yy-mm-dd" }));
         });
  
  &lt;/script&gt;
  
  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
  &lt;title&gt;Add Letter&lt;/title&gt;
  &lt;style type="text/css" media="screen"&gt;
      @import "style_contactform.css";
  &lt;/style&gt;
  &lt;/head&gt;    
  
  &lt;?php
  $querymethod = "select r_method_code, r_method from r_method order by r_method";
  $resultmethod = mysql_query($querymethod) or die ( mysql_error());
  
  $querybranch = "select bcode, branch from branch order by branch";
  $resultbranch = mysql_query($querybranch) or die ( mysql_error());
  
  $querytype = "select tcode, type from type order by type";
  $resulttype = mysql_query($querytype) or die ( mysql_error());
  
  ?&gt;       
  
  &lt;?php
  
  if  (isset($_POST["submit"]))
  {
  
  $rno =$_POST["rno"];  
  $lno =$_POST["lno"]; 
  $dol = mysql_real_escape_string($_POST["doi"]);
  $hdg =$_POST["hdg"];   
  $from =$_POST["from"];       
  $address =$_POST["address"];
  $method =$_POST["method"];
  $type =$_POST["type"];
  $branch =$_POST["branch"];
  
  if ((empty($hdg))){
  echo '&lt;script language="javascript"&gt;';
  echo 'alert("All fields must be required")';
  echo '&lt;/script&gt;';
  }
  else
  {
  $query ="INSERT INTO letter (reference_no, letter_no, date_stamp, heading, from_1, address, r_method_code, tcode, bcode) VALUES ('$rno', '$lno', '$dst', '$hdg', '$from', '$address', '$method', '$type', '$branch')";
     $result = mysql_query($query) or die ( mysql_error());
     $rc = mysql_affected_rows();
     echo '&lt;script language="javascript"&gt;';
     echo 'alert("Added Successfully")';
     echo '&lt;/script&gt;';
  }
  }
  ?&gt;
  &lt;html&gt;
  
  &lt;form id="contactform"&gt;
  
  &lt;div class="formcolumn"&gt;
  
  &lt;label for="rno"&gt;Reference No:&lt;/label&gt;
  &lt;input type="text" name="rno" /&gt;
  
  &lt;label for="lno"&gt;Letter No:&lt;/label&gt;
  &lt;input type="text" name="lno" /&gt;
  
  &lt;label for="dst"&gt;Date of the Letter:&lt;/label&gt;
  &lt;input type="text" name="dst" id="datepicker" /&gt;
  
  &lt;label for="hdg"&gt;Heading:&lt;/label&gt;
  &lt;textarea name="hdg"&gt;&lt;/textarea&gt;
  
  &lt;/div&gt;
  
  &lt;div class="formcolumn"&gt;
  
  &lt;label for="from"&gt;From 1:&lt;/label&gt;
  &lt;input type="text" id="from" /&gt;
  
  &lt;label for="address"&gt;Address:&lt;/label&gt;
  &lt;textarea id="address"&gt;&lt;/textarea&gt;
  
  &lt;label for="method"&gt;Received Method:&lt;/label&gt;
  &lt;select name="method" span  class="al"&gt;
   &lt;?php
  do { 
  ?&gt;
  &lt;option value="&lt;?php echo $rowmethod['r_method_code']?&gt;"&gt;&lt;?php echo $rowmethod['r_method']?&gt;&lt;/option&gt;
   &lt;?php
  } while ($rowmethod = mysql_fetch_assoc($resultmethod));
  ?&gt; 
  &lt;/select&gt;
  
  &lt;label for="type"&gt;Type:&lt;/label&gt;
  &lt;select name="type" span  class="al"&gt;
   &lt;?php
  do { 
  ?&gt;
  &lt;option value="&lt;?php echo $rowtype['tcode']?&gt;"&gt;&lt;?php echo $rowtype['type']?&gt;&lt;/option&gt;
   &lt;?php
  } while ($rowtype = mysql_fetch_assoc($resulttype));
  ?&gt; 
  &lt;/select&gt;
  
  &lt;label for="branch"&gt;Branch:&lt;/label&gt;
  &lt;select name="branch" span  class="al"&gt;
   &lt;?php
  do { 
  ?&gt;
  &lt;option value="&lt;?php echo $rowbranch['bcode']?&gt;"&gt;&lt;?php echo $rowbranch['branch']?&gt;&lt;/option&gt;
   &lt;?php
  } while ($rowbranch = mysql_fetch_assoc($resultbranch));
  ?&gt; 
  &lt;/select&gt;
  
  &lt;/div&gt;
  
  &lt;div class="buttons"&gt;
  &lt;input class="button" type="submit" value="Submit!" /&gt;
  
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>But I was unable to add records through this form to the relevant table. I can not understand what I am going wrong. Can any one help me?... Pls...</p>