我的问题是关于通过从表中选择单个记录并使用完整数据转到另一个页面中的所选记录来更新某个记录的能力。我知道我必须用我需要的所有输入字段创建另一个文件,但我的问题是如何到达并发送我选择的数据信息,然后如何回显该信息并允许记录到要更新?
假设我有一个名为“Products”的表,如下所示:
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>
答案 0 :(得分:3)
您可以使用隐藏字段
将表单数据发送到下一页<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'];
答案 1 :(得分:1)
<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...
}