我想通过从下拉列表中存储的Products
中检索所有折扣代码来更新Discounts
中的折扣代码,并让用户在{中选择新代码或重置有效折扣代码{1}}。
Html表格显示所有带有Products
(product_discount
)加入折扣代码(Discounts
)的产品。将discount_code
重置为“无折扣”工作正常,但我的下拉product_discount
总是空的,我很难过。谢谢。
$_Post['discount']
对不起家伙我弄错了#@ ^^ Html select未正确包装在每个产品的表单中。 这是工作代码:
<?php
session_start();
if (isset($_POST['change'])) { // is OK
$change = $_POST['change'];
if (!empty($_POST['discount'])) { // PROBLEM : always empty and never updated
$new_discount = $_POST['discount'];
// All DB stuff config/try goes here
$updt = $connexion -> prepare(
"UPDATE Products SET product_discount = '$new_discount'
WHERE productID = '$change'"
);
$updt->execute();
$connexion = null;
// All DB stuff catch goes here
}
}
if (isset($_POST['reset'])) { // is OK
$reset = $_POST['reset'];
$no_discount = 'no discount';
// All DB stuff config/try goes here
$updt = $connexion -> prepare(
"UPDATE Products SET product_discount = '$no_discount'
WHERE productID = '$reset'"
);
$updt->execute(); // Update OK
$connexion = null;
// All DB stuff catch goes here
}
?>
...
<body>
<?php
// All DB stuff config/try goes here
// Join tables
$select = $connexion -> prepare(
"SELECT
Products.productID,
Products.product_name,
Products.product_price,
Products.product_discount,
Discounts.discount_code,
Discounts.discount_desc,
Discounts.discount_percent,
FROM Products
INNER JOIN Discounts ON Products.product_discount = Discounts.discount_code"
);
$select->execute();
$resultat = $select->fetchall();
// Html table displays only products on discount
$display_discounts .= '<p style="font-size: 20px; text-align: center;">Active discounts</p>
<form method="post" action="">
<table cellpadding="0" cellspacing="0" align="center" class="db-table">
<tr>
<th>Product</th>
<th>Public price</th>
<th>Discount price</th>
<th>Discount code</th>
<th>Description</th>
<th>New code</th>
<th> </th>
<th> </th>
</tr>';
foreach($resultat as $row) // loop on Products
{
$discount_price = $row['product_price'] - ($row['product_price'] * $row['discount_percent'] );
$display_discounts .= '<tr>
<td>'.$row['product_name'].'</td>
<td>'.$row['product_price'].'</td>
<td>'.$discount_price.'</td>
<td>'.$row['product_discount'].'</td>
<td>'.$row['discount_desc'].'</td>';
// populate discount codes in drop-down
$select_d = $connexion -> prepare(
"SELECT
discount_code
FROM Discounts"
);
$select_d->execute();
$result_d = $select_d->fetchall();
$display_discounts .= "<td><select id='discount' name='discount'>";
foreach($result_d as $row_d) { // loop on Discounts
$display_discounts .= "<option value='".$row_d['discount_code']."'>".$row_d['discount_code']."</option>";
// value is well populated but empty in $_POST['discount']
$display_discounts .= '</select></td>
<td><button class="btn btn-danger bold" type="submit" name="change" value="'.$row['productID'].'">Change</button></td>
<td><button class="btn btn-danger bold" type="submit" name="reset" value="'.$row['productID'].'">Reset</button></td>
</tr>';
}
$display_discounts .= '</table></form>';
echo $display_discounts;
$connexion = null;
...
</body>
带有工作代码和干净HTML(CSS&amp; PHP)的新版本
CSS
...
$result_d = $select_d->fetchall();
// ----------->> start form
$display_discounts .= "<form action='' method='post'><td><select id='discount' name='discount'>";
foreach($result_d as $row_d) { // loop on Discounts
$display_discounts .= "<option value='".$row_d['discount_code']."'>".$row_d['discount_code']."</option>";
}
$display_discounts .= '</select></td>
<td><button class="btn btn-danger bold" type="submit" name="change" value="'.$row['productID'].'">Change</button></td>
<td><button class="btn btn-danger bold" type="submit" name="reset" value="'.$row['productID'].'">Reset</button></td></tr>
// ----------->> end form
</form>';
} // end loop Products
$display_discounts .= '</table>';
echo $display_discounts;
PHP
div.table {
display: table;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
div.thead {
display: table-header-group;
font-weight: bold;
background: #eee;
padding: 5px;
text-align: center;
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;
}
form.tr,
div.tr {
display: table-row;
}
span.td,
span.th {
display: table-cell;
padding: 5px;
text-align: center;
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;
}