in de database te krijgen, er komt altijd 0 in te staan.
de andere waarden komen er wel correct in te staan.
wie kan mij hiermee helpen?
<?php
$errorMessages = [];
// Haal de waarde van de query parameter op
$coupon_code = $_POST['coupon_code'] ?? '';
if ($coupon_code) {
$currentDate = date('Y-m-d');
$query_coupon = "SELECT discount, status, date_start, date_end FROM oc_coupon WHERE code = '$coupon_code'";
$result_coupon = mysqli_query($link, $query_coupon);
if ($result_coupon === false) {
die("Query faalde: " . mysqli_error($link));
}
if (mysqli_num_rows($result_coupon) > 0) {
$row_coupon = mysqli_fetch_assoc($result_coupon);
$korting = $row_coupon['discount'];
$status = $row_coupon['status'];
$date_start = $row_coupon['date_start'];
$date_end = $row_coupon['date_end'];
if ($status != 1) {
$errorMessages[] = "De kortingscode is niet actief.";
}
if ($currentDate < $date_start) {
$errorMessages[] = "De kortingscode is nog niet geldig.";
}
if ($currentDate > $date_end) {
$errorMessages[] = "De kortingscode is verlopen.";
}
} else {
$errorMessages[] = "De kortingscode bestaat niet.";
}
}
if (empty($errorMessages)) {
// Haal totaal_bedrag op uit de bestellingen tabel
$query_bestelling = "SELECT totaal_bedrag FROM bestellingen WHERE order_id = $order_id";
$result_bestelling = mysqli_query($link, $query_bestelling);
if ($result_bestelling && mysqli_num_rows($result_bestelling) > 0) {
$row_bestelling = mysqli_fetch_assoc($result_bestelling);
$totaal_bedrag = $row_bestelling['totaal_bedrag'];
// Bereken het totaal met korting
$totaal_met_korting = $totaal_bedrag - ($totaal_bedrag * ($korting / 100));
// Update query om de waarden op te slaan in de database
$updateQuery = "UPDATE bestellingen SET kortings_code = '$coupon_code', korting_percentage = $korting, totaal_bedrag = $totaal_bedrag, totaal_met_korting = $totaal_met_korting WHERE order_id = $order_id";
if (mysqli_query($link, $updateQuery)) {
echo "<p class='success-message'>Bijgewerkt in de database.</p>";
} else {
echo "<p class='error-message'>" . mysqli_error($link) . "</p>";
}
}
} else {
foreach ($errorMessages as $message) {
echo "<p class='error-message'>$message</p>";
}
}
mysqli_close($link);
?>
<form action="" method="POST">
<div class="form-group">
<label for="coupon_code">Voer een kortingscode in:</label>
<input type="text" name="coupon_code" id="coupon_code" class="form-control" required>
</div>
<div class="form-actions">
<button type="submit" name="submit" class="btn btn-primary">Controleer Kortingscode</button>
</div>
</form>
<?php if ($coupon_code): ?>
<div class="result">
<?php if (empty($errorMessages)): ?>
<p class="success-message">super! je krijgt <?php echo htmlspecialchars($korting); ?>% korting</p>
<?php else: ?>
<?php foreach ($errorMessages as $message): ?>
<p class="error-message"><?php echo htmlspecialchars($message); ?></p>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endif; ?>
</td>
</tr>
<tr>
<th>Totaal: </th>
<td>
<?php
// Bereken het bedrag inclusief korting
$totaal_bedrag = $totaal - ($totaal * ($korting / 100));
// Gebruik number_format om het resultaat te formatteren
$totaal_met_korting = number_format($totaal_bedrag, 2, ',', '');
// Output
echo "<p>Sub totaal: € " . number_format($totaal, 2, ',', '') . "</p>";
echo "<p>Korting: -$korting%</p>";
echo "<p>Totaal te betalen: € $totaal_met_korting</p>";
// Sluit de verbinding
mysqli_close($link);
?>