创建优惠券代码


Create Coupon Code

我正在开发一个没有使用集体"解决方案"的购物网站。我想让客户收到你账户和他账户Panel邮件中的凭证,他有一张优惠券可以打印。每个优惠券发生,你必须有一个唯一的代码,每个客户。有人知道该怎么做吗?

我使用这个代码

while (true) {
    $ccode = "CP" . strtoupper ( dechex ( rand ( 1000000000, 9999999999) ) ) . strtoupper ( chr( rand( 65, 90 ) ) ) . chr(rand(65, 90));
    $check = mysql_query("SELECT coupon_code FROM item_coupons WHERE coupon_code = '$ccode'");
    if (mysql_num_rows($check) != 0) {
        //duplicate
    } else {
        break 1;
    }
}

他的电子邮件id的哈希值可以用作优惠券代码。并将其存储在数据库中,以避免欺诈。

使用php条形码制作器,这就是我要做的

首先,您可以在数据库中创建一个coupon表,如下所示:

create table `coupon`(
    `code` char(32) primary key,
    `email` varchar(255),
    `used` tinyint(1) default 0
) ENGINE=MYISAM;

现在您可以生成完全随机的优惠券,然后将其保存在表中以供以后检查:

function generate_coupon_for($email)
{
    do
    {
        $cpn = md5(rand());
        if(mysql_query("insert into `coupons`(`code`, `email`) values('$cpn', '$email');"))
        {
            return $cpn;
        }
    }while(mysql_errno() == 1062);
    //We had an error while trying to insert the coupon
}

通过这种方式,您可以查看优惠券:

function check_coupon($code, $email)
{
    $q = mysql_query("update `coupons` set `used` = 1 where `email`='$email' and `code`='$code' and `used`=0;");
    return mysql_affected_rows() > 0;
}

它将返回true或false…