admin

在SQL WHERE中添加PHP变量

sql

在我的第一个UPDATE语句上,我正在尝试让我的WHERE值包含变量$
couponCode,但到目前为止它不起作用。这样,正确的行将根据输入内容进行更新。任何帮助,将不胜感激。

if ($couponCode == $coupons_db3['coupon_code']){
   echo $couponCode;
   $stmt = $db->prepare('UPDATE promocode_3 SET used = 1 WHERE coupon_code ='.$couponCode);
   $stmt = $db->prepare('UPDATE usr_customer_profile SET packageid = 3 WHERE usrcustomerid = :usrcustomerid');
   $stmt->bindValue(':usrcustomerid', $_SESSION['usrcustomerid'], PDO::PARAM_INT);
   $stmt->execute();
   break;
 }

阅读 253

收藏
2021-07-01

共1个答案

admin

您还需要绑定couponCode

if ($couponCode == $coupons_db3['coupon_code']){
       echo $couponCode;
       $stmt = $db->prepare('UPDATE promocode_3 SET used = 1 WHERE coupon_code =:couponCode');
       $stmt->bindValue(':couponCode', $couponCode, PDO::PARAM_STR);
       $stmt->execute();

       $stmt = $db->prepare('UPDATE usr_customer_profile SET packageid = 3 WHERE usrcustomerid = :usrcustomerid');
       $stmt->bindValue(':usrcustomerid', $_SESSION['usrcustomerid'], PDO::PARAM_INT);
       $stmt->execute();
       break;
     }
2021-07-01