【问题标题】:Insert and edit the data using same form in php在 php 中使用相同的表单插入和编辑数据
【发布时间】:2016-06-17 17:39:26
【问题描述】:

我有页面 (auditplanentry.php),其中包含用于输入数据的表单控件。输入数据后,使用同一 php 文件中的 PDO 查询将数据保存在 mysql 中。 我将所有数据显示为另一个 PHP 页面(auditplan.php)中的表格。在每一行中,我都有编辑按钮,单击它后,另一个 php 页面 (auditplanedit.php) 具有相同的表单控件,其中填充了来自 mysql 的数据。编辑后,我在同一 auditplanedit.php 页面中使用更新查询在 mysql 中更新。 在这里,我想知道,如何使用相同的 auditplanentry.php 页面进行输入和更新。这里我在另一个页面中重复相同的表单控件编码,除了我将值分配给来自 mysql 的控件。

给我一​​些想法,怎么做?

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();
            }   

审计计划编辑.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");

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>

【问题讨论】:

  • 如果操作是编辑,那么您可以在页面auditplanentry.php 上将$_GET['action'] 参数设置为edit。如果$_GET['action'] 设置为编辑,则获取记录并显示在文本框中并更改提交按钮的名称。在auditplan.php 之后检查是否发布了编辑操作并执行更新/插入。
  • 您可以在提及您的操作的地方使用隐藏的文本字段..“新”或“更新”.. 相应地填充该字段。
  • 您熟悉INSERT...UPDATE 语法吗?

标签: php mysql forms


【解决方案1】:

在您的布局中.. 只需在要在表单中显示的值之前插入符号 @。插入@符号告诉编译器在页面调用添加新数据时抑制由于未声明的变量而导致的错误。因此,您的页面将如下所示...

【讨论】:

    【解决方案2】:

    您可以使用$_GET 参数让表单知道您是否正在编辑。

    因此,您的 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();
          }     
      }
    }
    
    ?>
    

    您的 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>
    

    你的链接看起来像:

    <a href="script.php/">add</a>
    <a href="script.php?act=edit&cid=<?php echo $getId; ?>">Edit</a>
    

    【讨论】:

      【解决方案3】:

      您可以从 url 获取 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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        相关资源
        最近更新 更多