Insert and edit the data using same form in php

13,048

You can use a $_GET parameter to let the form know if you are editing or not.

So, you PHP code will look something like this:

<?php 

if($_GET['act']=="edit"){ // If $_GET['act'] equals to 'edit'
    // Select query

try {
   $sql = "SELECT * FROM auditplan WHERE id = :cid";
   $stmt = $DB->prepare($sql);
   $stmt->bindValue(":cid",intval($_GET['cid']));   
   $stmt->execute();
   $results = $stmt->fetchAll();
} catch (Exception $ex) {
  echo $ex->getMessage();
}

  if(isset($_POST['submit'])){
    // Edit query  
    $sql = "UPDATE auditplan SET auditid = :audit, year = :year, month = :month, status = :status, comment = :comment" . " WHERE id = :cid ";       

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

}else{ // if $_GET['act'] doesn't equal to 'edit'

if(isset($_POST['submit'])){

    // Add query    
  $sql = "INSERT INTO auditplan(auditid,year,month,status,comment) VALUES " . "(:audit, :year, :month, :status, :comment)";
  try {           
        $stmt = $DB->prepare($sql);               
        $stmt->bindValue(":audit", $audit);
        $stmt->bindValue(":year", $year);
        $stmt->bindValue(":month", $month);
        $stmt->bindValue(":status", $status);
        $stmt->bindValue(":comment", $comment); 
        // execute Query
        $stmt->execute();          
      } 
      catch (Exception $ex)
      {
        $_SESSION["errorType"] = "danger";
        $_SESSION["errorMsg"] = $ex->getMessage();
      }     
  }
}

?>

You HTML form will look something like this:

<form method="POST">
  <input type="text" name="day" value="<?php echo isset($_GET['act']) && $_GET['act']=="edit" ? $results[0]["day"] : ""; ?>" />
  <input type="text" name="month" value="<?php echo isset($_GET['act']) && $_GET['act']=="edit" ? $results[0]["month"] : ""; ?>" />
  <input type="submit" name="submit"/>
</form>

And you links would look something like:

<a href="script.php/">add</a>
<a href="script.php?act=edit&cid=<?php echo $getId; ?>">Edit</a>
Share:
13,048
Anu
Author by

Anu

Working in VC++. Got a chance to work in exploring Open SOurce. Now working in PHP, MySQL.

Updated on June 15, 2022

Comments

  • Anu
    Anu almost 2 years

    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.

    Just give me some idea,how to do that.?

    auditplanentry.php

    <div class="form-group">
      <label class="control-label col-sm-2" for="pwd">Year:</label>
        <div class="col-sm-5">
            <input type="text" class="form-control col-xs-3" id="year" name ="year">
        </div>
    </div>
    
    <div class="form-group">
      <label class="control-label col-sm-2" for="usr">Month:</label>
        <div class="col-sm-5">
            <input  type="text" class="form-control" id="month" name ="month">
        </div>
    </div>
    
    //query code:
    
    $sql = "INSERT INTO auditplan(auditid,year,month,status,comment) VALUES " . "(:audit, :year, :month, :status, :comment)";
    
            try {           
                  $stmt = $DB->prepare($sql);               
                  $stmt->bindValue(":audit", $audit);
                  $stmt->bindValue(":year", $year);
                  $stmt->bindValue(":month", $month);
                  $stmt->bindValue(":status", $status);
                  $stmt->bindValue(":comment", $comment); 
                  // execute Query
                  $stmt->execute();          
                } 
                catch (Exception $ex)
                {
                  $_SESSION["errorType"] = "danger";
                  $_SESSION["errorMsg"] = $ex->getMessage();
                }   
    

    auditplanedit.php

    <?php
    include("config.php"); 
    include("header.php"); 
     try {
       $sql = "SELECT * FROM auditplan WHERE id = :cid";
       $stmt = $DB->prepare($sql);
       $stmt->bindValue(":cid",intval($_GET['cid']));   
       $stmt->execute();
       $results = $stmt->fetchAll();
    } catch (Exception $ex) {
      echo $ex->getMessage();
    }
    
    ?>    
    <div class="form-group">
          <label class="control-label col-sm-2" for="pwd">Year:</label>
            <div class="col-sm-5">
                <input type="text" class="form-control col-xs-3" id="year" name ="year" value="<?php echo $results[0]["year"] ?>">
            </div>
        </div>
    
        <div class="form-group">
          <label class="control-label col-sm-2" for="usr">Month:</label>
            <div class="col-sm-5">
                <input  type="text" class="form-control" id="month" name ="month" value="<?php echo $results[0]["month"] ?>">
            </div>
        </div>
    
    //database query:
    $sql = "UPDATE auditplan SET auditid = :audit, year = :year, month = :month, status = :status, comment = :comment" . " WHERE id = :cid ";       
    
            try {           
                  $stmt = $DB->prepare($sql);            
                  $stmt->bindValue(":audit", $audit);
                  $stmt->bindValue(":year", $year);
                  $stmt->bindValue(":month", $month);
                  $stmt->bindValue(":status", $status);
                  $stmt->bindValue(":comment", $comment); 
                  $stmt->bindValue(":cid", $cid);            
                  $stmt->execute();
                } 
                catch (Exception $ex)
                {                 
                  $_SESSION["errorMsg"] = $ex->getMessage();
                }
            header("Location:auditplan.php");
    

    Buttons in auditplan.php:

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