I'd like to update a discount code in Products
by retrieving all discount codes from Discounts
stored in a drop-down and let the user select a new code or reset active discount code in Products
.
Html table displays all products with a discount code (product_discount
) joined with Discounts
(discount_code
). Reseting product_discount
to 'no discount' is working fine but I'm getting hard times with my drop-down $_Post['discount']
always empty. Thanks.
<?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>
Sorry guys I made a mistake #@^^ Html select wasn't correctly wrapped in a form for each product. Here is the working code :
...
$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;
Aucun commentaire:
Enregistrer un commentaire