【问题标题】:Data not transferred between 2 forms using php-echo未使用 php-echo 在 2 个表单之间传输数据
【发布时间】:2019-04-25 08:47:46
【问题描述】:

我正在努力以 html 形式制作更新功能。我正在通过echo 将数据从一种形式发送到另一种形式以进行更新。但是我无法通过捕获从第一种形式发送的数据来填充其他数据。

字段 id 文本框为空白。

这是我的代码:

从 1 发送密钥:

  <tr>
    <td style='width:150px;border:1px solid grey;'><?= $row['field_id'] ?></td>
    <td style='width:150px;border:1px solid grey;'><?= $row['description'] ?></td>
    <td style='width:150px;border:1px solid grey;'><?= $row['corner_points'] ?></td>
    <td style='width:150px;border:1px solid grey;'><?= $row['notes'] ?></td>
    <td style='width:150px;border:1px solid grey;'><a target="_blank" href="Edit/edit-field.php?field_id=<?php echo $row['field_id'];?>">Edit</a></td>
  </tr>

接收数据edit-field.php:

<?php

    if(isset($GET["field_id"]))    
      {
       $field_id = $GET["field_id"]; 
      }

     try {
            $stmt = $conn->prepare("SELECT field_id, description, corner_points, notes FROM fields where field_id = $field_id");
            $stmt->execute();

            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $result = $stmt->fetchAll();
        }

        catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
        }
        ?>

   <form action="" method="post" class="needs-validation" novalidate enctype="multipart/form-data"> 
    <h6> Field </h6>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">Field ID</label>
      <input type="number" class="form-control" name="field_id" id="field_id" value="<?php echo $result["field_id"];?>">
      </div>
   </div>

  <button class="btn btn-primary btn-sm" name="submit1" type="submit">Update Records</button>
</form>

【问题讨论】:

    标签: php html forms pdo


    【解决方案1】:

    在 edit-field.php 中,通过 URL 传递的参数变量是 $_GET 而不是 $GET。

    将该文件的开头更改为:

    if(isset($_GET["field_id"])) {
       $field_id = $_GET["field_id"]; 
    }
    

    【讨论】:

      【解决方案2】:

      您不需要执行循环来获取结果吗?就像这个例子:

          <?php
      // $pdo est un objet PDO
      try{
          $stmt = $pdo->query('SELECT * FROM table');
          $rows = $stmt->fetchAll();
      }
      catch(Exception $e)
      {
          exit('<b>Catched exception at line '. $e->getLine() .' (code : '. $e->getCode() .') :</b> '. $e->getMessage());
      }
      foreach($rows as $row) {
          var_dump($row);
          echo '<br />';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-14
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多