My question is regarding the ability to update a certain record by choosing a single record from a table and going to that selected record in another page with the full data. I know I would have to create another file with all of the input fields I need, but my question is how do I get there and send over the data info that I selected, then how do I echo out that information and allow the record to be updated?
Let's say I have a table called "Products" that looks like this:
ID Name Amount
1 Shoes $10 Edit
2 Hats $5 Edit
If I click the "Edit" Button next to "Shoes" I want to go to a different page which allows me to edit all of the information for that record selected.
<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>
You can send your form data to next page using hidden field as
<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>
And in edit_page.php
use
$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>
And then in 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...
}