如何将特定的数据库记录发送到其他文件


How to send specific database records to other files

我的问题是关于通过从表中选择一条记录并转到另一个页面中具有完整数据的所选记录来更新特定记录的能力。我知道我必须用我需要的所有输入字段创建另一个文件,但我的问题是,我如何到达那里并发送我选择的数据信息,然后我如何回显这些信息并允许更新记录?

假设我有一个名为"产品"的表格,看起来像这样:

ID Name Amount
1 Shoes $10 Edit
2 Hats $5 Edit

如果我点击"鞋子"旁边的"编辑"按钮,我想转到另一个页面,该页面允许我编辑所选记录的所有信息。

  <form method="POST">
   <input name="first" placeholder="First Name">
   <input name="last" placeholder="Last Name">
   <input name="product" placeholder="Product">
   <button name="add" type="submit">Add</button>
</form>
</div>
<hr>
<table>
<thead>
   <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Amount</th>
      <th></th>
   </tr>
</thead>
<tbody>
   <?php
      $stmt = $dbc->query("SELECT * FROM users");
      $stmt->setFetchMode(PDO::FETCH_ASSOC);
      while($row = $stmt->fetch()) {
      ?>
   <form method="POST">
      <tr>
         <td><?php echo $row['id'];?></td>
         <td><?php echo $row['name'];?></td>
         <td><?php echo $row['amount'];?></td>
         <td><button name="edit" type="submit">Edit</button></td>
      </tr>
   </form>
   <?php } ?>
</tbody>

您可以使用隐藏字段作为将表单数据发送到下一页

<form method="POST" action="edit_page.php">// add action here
        <tr>
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['name'];?></td>
            <td><?php echo $row['amount'];?></td>
            <input name="name" type="hidden" value="<?php echo $row['name'];?>">
            <input name="id" type="hidden" value="<?php echo $row['id'];?>">
            <input name="amount" type="hidden" value="<?php echo $row['amount'];?>">
            <td><button name="edit" type="submit">Edit</button></td>
        </tr>
    </form> 

并且在edit_page.php中使用

$name=$_POST['name'];
$id=$_POST['id'];
$amount=$_POST['amount'];
<form method="POST" action="other_page.php">
        <tr>
           <input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
           <input type="hidden" name="name" value="<?php echo $row['name'] ?>" />
           <input type="hidden" name="amount" value="<?php echo $row['amount'] ?>" />
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['name'];?></td>
            <td><?php echo $row['amount'];?></td>
            <td><button name="edit" type="submit">Edit</button></td>
        </tr>
</form> 

然后在other_page.php中:

<?php
    $stmt = $dbc->query("SELECT * FROM products WHERE `id`=".$_POST['id'].";");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    ?>

    <form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
        <input type="text" name="id" value="<?php echo $row['id'] ?>" />
        <input type="text" name="name" value="<?php echo $row['name'] ?>" />
        <input type="text" name="amount" value="<?php echo $row['amount'] ?>" />
        <button name="update" type="submit">Edit</button>
    </form>
    <?php
    if (isset($_POST['update'])){ // if the update button is clicked
        // write your update query here, with $id = $_POST['id'] and so on...
    }