I have two pages - 1. products | 2. edit-products.
I have a table on the products page with products listed. I then have an edit button next to each product. When I click on the edit button, it takes the user to the edit-products page. Then I have additional input fields that the user can edit the information.
My issue is I cannot get my UPDATE
query
to work. It says I have an undefined index for the variables that I am bringing the information over with, but I do not understand that because I use those variables within the input fields.
This is my products page and how I go to the edit page:
$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()) {
?>
<form method="POST" action="edit-product">
<tr>
<td><?php echo $row['id'];?>"</td>
<td><?php echo $row['first'];?></td>
<td><?php echo $row['last'];?></td>
<td><?php echo $row['product'];?></td>
<input name="id" type="hidden" value="<?php echo $row['id'];?>" readonly>
<input name="first" type="hidden" value="<?php echo $row['first'];?>">
<input name="last" type="hidden" value="<?php echo $row['last'];?>">
<input name="product" type="hidden" value="<?php echo $row['product'];?>">
<td><div class="delete-class" name="delete" id="<?php echo $row['id']; ?>">Delete</div></td>
<td><input name="edit" type="submit" value="Edit"></td>
<!-- <td><a href="edit-product">Edit</a></td> -->
</tr>
</form>
<?php } ?>
</tbody>
Then on the edit page it is basically set up like this:
ini_set('display_errors', 1);
error_reporting(E_ALL);
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
$newId = filter_var($id, FILTER_SANITIZE_STRING);
try {
$host = 'localhost';
$name = '';
$user = '';
$password = '';
$dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);
}catch(PDOException $e) {
echo $e->getMessage();
}
if(isset($_POST['update'])) {
$stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");
$stmt->bindParam(1, $_POST['first']);
$stmt->bindParam(2, $_POST['last']);
$stmt->bindParam(3, $_POST['product']);
$stmt->bindParam(4, $_POST['amount']);
$stmt->bindParam(5, $_POST['available']);
$stmt->bindParam(6, $newId);
$stmt->execute();
}
$stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
//print_r($stmt);
while($row = $stmt->fetch()) {
?>
<form method="POST">
<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">
<button name="update" type="submit">Update Product Information</button>
</form>
When I hit the "Update" button to initiate the UPDATE query
I get the invalid index errors for the variables I have at the top of this page. These:
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
If I am sending over the information correctly with those variables, why would I be getting an error saying they are undefined when I try to do this query?
Is there something else wrong in my code or placement issues?
UPDATED edit page
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
$newId = filter_var($id, FILTER_SANITIZE_STRING);
try {
$host = 'localhost';
$name = '';
$user = '';
$password = '';
$dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);
}catch(PDOException $e) {
echo $e->getMessage();
}
$stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
//print_r($stmt);
while($row = $stmt->fetch()) {
?>
<form name="update" method="POST">
<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">
<button type="submit">Update Product Information</button>
</form>
<?php
}
if(isset($_POST['update'])) {
$stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");
$stmt->bindParam(1, $_POST['first']);
$stmt->bindParam(2, $_POST['last']);
$stmt->bindParam(3, $_POST['product']);
$stmt->bindParam(4, $row['amount']);
$stmt->bindParam(5, $row['available']);
$stmt->bindParam(6, $newId);
$stmt->execute();
}
?>