简体   繁体   中英

Insert and edit the data using same form in php

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>

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>

you can get the id from url to decide it is edit/create action and then you can decide which fields to show/hide, like below

if(isset($_GET['id'])) 
{
   //  get record from table of that id and show into form
   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();
   }
   // Show your form fields here.like below
  ?>
       <input type="text" class="form-control col-xs-3" id="year" name ="year" value="<?php echo $results[0]["year"] ?>">
  <?php

} 
else 
{
    // Its create action , so show your form fields like
 ?>
     <input type="text" class="form-control col-xs-3" id="year" name ="year">
 <?php
}

In your layout.. Just insert the symbol @ before the values you want to display in the form. Inserting @ symbol tells the compiler to suppress error due to undeclared variable when the page called for adding new data. Hence your page will look like this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM