简体   繁体   English

在php中使用相同的表单插入和编辑数据

[英]Insert and edit the data using same form in php

I have the page (auditplanentry.php) ,that contains form controls to enter data. 我有页面(auditplanentry.php),其中包含用于输入数据的表单控件。 after enntering the data get saved in mysql using PDO query in the same php file. 输入数据后,在同一个php文件中使用PDO查询将数据保存在mysql中。 Im displaying all data as a table in another PHP page (auditplan.php). 我在另一个PHP页面(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. 在每一行中,我都有一个编辑按钮,单击它后是另一个php页面(auditplanedit.php),该页面具有相同的表单控件,其中填充了来自mysql的数据。 After editing that,im updating in mysql using Update query in same auditplanedit.php page. 编辑后,在同一auditplanedit.php页面中使用Update查询在mysql中进行更新。 Here i want to know, how i can use same auditplanentry.php page both for entry and updating. 在这里我想知道,我如何使用相同的auditplanentry.php页面进行输入和更新。 Here im repeating the same form controls coding in another page except im assinging values to controls from mysql. 在这里,我将相同的表单控件重复在另一个页面中进行编码,除了将值从mysql赋值给控件。

Just give me some idea,how to do that.? 请给我一些想法,怎么做?

auditplanentry.php 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 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: 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. 您可以使用$_GET参数让表单知道您是否正在编辑。

So, you PHP code will look something like this: 因此,您的PHP代码将如下所示:

<?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: 您的HTML表单将如下所示:

<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 您可以从网址获取ID来确定它是编辑/创建操作,然后确定要显示/隐藏的字段,如下所示

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... 因此,您的页面将如下所示...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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