这是我应该执行的代码的摘要:
到目前为止,我的if语句总是直接直接转到即使找到匹配也找不到匹配的部分,从而仅显示“回显'Coupon代码不存在!';”
这是表格(名为“ promocode_3”)
我从一个函数开始: (PromoCode.mc.php)
<?php
Class PromoCode_MC {
function getCoupons() {
global $db;
try {
$stmt = $db->prepare("SELECT * FROM promocode_3 WHERE `used` = 0");
$stmt->execute();
return $stmt->fetchall();
} catch (Exception $e) {
echo $e->getMessage();
} } } ?>
然后id从另一个文件中调用该函数 (membership.vc.php)
require_once($routePath . "_mc/PromoCode.mc.php");
$mcPromoCode = new PromoCode_MC();
$coupons = $mcPromoCode->getCoupons();
最后我的第三个文件将获得该功能并显示回显 (membership.php)
<?php
session_start();
require_once('membership.vc.php');
?>
<form method="post">
<input type="text"id ="lux_code" name="lux_code" placeholder="ENTER PROMO CODE" style="text-align:center;" autocomplete="off">
<input type="submit" class="full_width btn color-white mwc-orange-background-color" name="redeem" value="REDEEM">
</form>
<?php
$coupons = ['coupon_code'];
if (isset($_POST['redeem']) && $_POST['redeem'] == 'REDEEM'){
$couponCode = $_POST['lux_code'];
$match = false;
foreach($coupons as $coupon){
if($coupon == $couponCode) { //check to see if posted value matches
$match = true;
echo 'Coupon code exists!';
$stmt2 = $db->prepare("UPDATE coupons SET used = '1' WHERE p3id = :p3id");
$stmt2->execute(array(
':p3id'=>$coupon['p3id']
));
break;
} else {
echo 'Coupon code does not exist!';
}
}
}
?>
对发现错误的任何帮助将不胜感激
更新:
即使我只是试图根据比较来整理回声,我仍然没有得到“回声优惠券代码存在!”;如果我输入的字符串值应与表格中的coupon_code列之一匹配
if (isset($_POST['redeem']) && $_POST['redeem'] == 'REDEEM'){
$couponCode = $_POST['lux_code'];
foreach($coupons as $coupon){
if($coupon == $couponCode) { //check to see if posted value matches
echo 'Coupon code exists!';
} else {
echo 'Coupon code does not exist!';
}
}
}