I created an admin function to be able to display/add/edit products that are on my site. I am having no issues with adding and fetching/displaying what products are on my site.
Being able to see the current product I am trying to edit is where I am having difficulties. I am displaying the products that are in my database by fetching all of them through a while loop. I added an edit link that goes to editproducts.php.
The problem I am having is I am not sure how to get the product's data that I clicked on to display and fetch just that product_id's data.
The way I display all of the products
<table class="tableproduct">
<tr>
<th class="thproduct">Product ID</th>
<th class="thproduct">Product Name</th>
<th class="thproduct">Price</th>
<th class="thproduct">Category</th>
<th class="thproduct">Description</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if(isset($_POST['product_id']) && is_numeric($_POST['product_id'])) {
mysqli_query($con, "DELETE FROM products WHERE product_id = ". $_POST['product_id'] ."")
or die("Could not DELETE: " . mysqli_error($con));
"Your product was successfully deleted.";
} else {Session::flash('addproduct', 'Your product was successfully deleted.');
}
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="adminproducts.php">
<tr>
<td class="tdproduct"><?php echo $row['product_id']; ?> </td>
<td class="tdproduct"><?php echo $row['name']; ?> </td>
<td class="tdproduct"><?php echo $row['price']; ?> </td>
<td class="tdproduct"><?php echo $row['category']; ?> </td>
<td class="tdproduct"><?php echo $row['description']; ?> </td>
<td class="tdproduct"><a href='editproducts.php?product_id=<?php echo $row['product_id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['product_id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
The page where I want to edit the product.
I tried the GET function and pulling the product_id. This does nothing. The name and price that I am trying to echo at the bottom of the code does not display anything other than the echoed string '$'.
<?php
$_GET['product_id'];
?>
<form action="" method="post">
<div class="field">
<label for="product_id">Product ID</label>
<input type="text" name="product_id" class="inputbar">
</div>
<div class="field">
<label for="name">Product Name</label>
<input type="text" class="inputbar" name="name">
</div>
<div class="field">
<label for="price">Price</label>
<input type="text" class="inputbar" name="price">
</div>
<div class="field">
<label for="category">Category</label>
<input type="text" class="inputbar" name="category">
</div>
<div class="field">
<label for="description">Description</label>
<input type="text" class="inputbar" name="description">
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<label for="signinButton">
<input type="submit" id="signinButton" value="Update">
</label>
</form>
<p><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'] . "</a>"; ?></p>
<p> <?php echo "$" . $product['price']; ?> </p>
Is there something I'm not doing correctly or over-looking?