Insert and edit the data using same form in php
<p>I have the page (auditplanentry.php) ,that contains form controls to enter data. after enntering the data get saved in mysql using PDO query in the same php file.
Im displaying all data as a table in another PHP page (auditplan.php). In that in each row i have edit button,after clicking it another php page (auditplanedit.php) with the same form controls filled with data from mysql. After editing that,im updating in mysql using Update query in same auditplanedit.php page.
Here i want to know, how i can use same auditplanentry.php page both for entry and updating. Here im repeating the same form controls coding in another page except im assinging values to controls from mysql.</p>

<p>Just give me some idea,how to do that.?</p>

<p>auditplanentry.php</p>

<pre><code>&lt;div class="form-group"&gt;
  &lt;label class="control-label col-sm-2" for="pwd"&gt;Year:&lt;/label&gt;
    &lt;div class="col-sm-5"&gt;
        &lt;input type="text" class="form-control col-xs-3" id="year" name ="year"&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div class="form-group"&gt;
  &lt;label class="control-label col-sm-2" for="usr"&gt;Month:&lt;/label&gt;
    &lt;div class="col-sm-5"&gt;
        &lt;input  type="text" class="form-control" id="month" name ="month"&gt;
    &lt;/div&gt;
&lt;/div&gt;

//query code:

$sql = "INSERT INTO auditplan(auditid,year,month,status,comment) VALUES " . "(:audit, :year, :month, :status, :comment)";

        try {           
              $stmt = $DB-&gt;prepare($sql);               
              $stmt-&gt;bindValue(":audit", $audit);
              $stmt-&gt;bindValue(":year", $year);
              $stmt-&gt;bindValue(":month", $month);
              $stmt-&gt;bindValue(":status", $status);
              $stmt-&gt;bindValue(":comment", $comment); 
              // execute Query
              $stmt-&gt;execute();          
            } 
            catch (Exception $ex)
            {
              $_SESSION["errorType"] = "danger";
              $_SESSION["errorMsg"] = $ex-&gt;getMessage();
            }   
</code></pre>

<p>auditplanedit.php</p>

<pre><code>&lt;?php
include("config.php"); 
include("header.php"); 
 try {
   $sql = "SELECT * FROM auditplan WHERE id = :cid";
   $stmt = $DB-&gt;prepare($sql);
   $stmt-&gt;bindValue(":cid",intval($_GET['cid']));   
   $stmt-&gt;execute();
   $results = $stmt-&gt;fetchAll();
} catch (Exception $ex) {
  echo $ex-&gt;getMessage();
}

?&gt;    
&lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-2" for="pwd"&gt;Year:&lt;/label&gt;
        &lt;div class="col-sm-5"&gt;
            &lt;input type="text" class="form-control col-xs-3" id="year" name ="year" value="&lt;?php echo $results[0]["year"] ?&gt;"&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-2" for="usr"&gt;Month:&lt;/label&gt;
        &lt;div class="col-sm-5"&gt;
            &lt;input  type="text" class="form-control" id="month" name ="month" value="&lt;?php echo $results[0]["month"] ?&gt;"&gt;
        &lt;/div&gt;
    &lt;/div&gt;

//database query:
$sql = "UPDATE auditplan SET auditid = :audit, year = :year, month = :month, status = :status, comment = :comment" . " WHERE id = :cid ";       

        try {           
              $stmt = $DB-&gt;prepare($sql);            
              $stmt-&gt;bindValue(":audit", $audit);
              $stmt-&gt;bindValue(":year", $year);
              $stmt-&gt;bindValue(":month", $month);
              $stmt-&gt;bindValue(":status", $status);
              $stmt-&gt;bindValue(":comment", $comment); 
              $stmt-&gt;bindValue(":cid", $cid);            
              $stmt-&gt;execute();
            } 
            catch (Exception $ex)
            {                 
              $_SESSION["errorMsg"] = $ex-&gt;getMessage();
            }
        header("Location:auditplan.php");
</code></pre>

<p>Buttons in auditplan.php:</p>

<pre><code>&lt;?php   $getId = $row["id"];?&gt;
                    &lt;td&gt; 
                    &lt;a href="auditplanedit.php?cid=&lt;?php echo $getId; ?&gt;"&gt;&lt;i class="ion ion-edit"&gt;&lt;/i&gt;&lt;/a&gt;&amp;nbsp&amp;nbsp &amp;nbsp&amp;nbsp;
                    &lt;a href="deleteauditplan.php?cid=&lt;?php echo $getId; ?&gt;" onclick="return confirm('Are you sure?')"&gt;&lt;i class="ion ion-close"&gt;&lt;/i&gt;&lt;/a&gt;&amp;nbsp;              
                    &lt;/td&gt;
</code></pre>