Php 在不同页面上显示while循环中特定id的信息
我创建了一个管理功能,可以显示/添加/编辑我网站上的产品。我在添加和获取/显示网站上的产品方面没有问题 能够看到我试图编辑的当前产品是我遇到困难的地方。我通过while循环获取所有产品来显示数据库中的产品。我添加了一个指向editproducts.php的编辑链接 我遇到的问题是,我不确定如何获取我单击以显示和获取该产品id数据的产品数据 我展示所有产品的方式Php 在不同页面上显示while循环中特定id的信息,php,html,mysql,Php,Html,Mysql,我创建了一个管理功能,可以显示/添加/编辑我网站上的产品。我在添加和获取/显示网站上的产品方面没有问题 能够看到我试图编辑的当前产品是我遇到困难的地方。我通过while循环获取所有产品来显示数据库中的产品。我添加了一个指向editproducts.php的编辑链接 我遇到的问题是,我不确定如何获取我单击以显示和获取该产品id数据的产品数据 我展示所有产品的方式 <table class="tableproduct"> <tr>
<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>
产品ID
品名
价格
类别
描述
产品ID
品名
价格
类别
描述
是否有我做得不正确或过度检查的地方?您指定
form method=“POST”
。表单变量将包含在$\u POST
或$\u REQUEST
中,但不包含$\u GET
。另外,您可能永远也不应该从数据库中删除产品-当然,要有一个“deleted”(已删除)标志,并设置它,但永远不要完全删除它。我将我的editproducts.php页面更改为$\u POST,但它仍然没有从页面中获取数据,直到您实际需要对变量执行某些操作——只需放置$\u POST['product\u id']
如果您不使用它,我不会做任何事情——在数据库中查找id,并将其回传到文档等等,这是我对如何处理我选择的特定产品感到困惑的部分。您删除它的方式相同吗?只需选择信息即可。
<?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>