I have a system in which when a user uses a coupon, a "taken" column is filled with a string which I run IS NULL against to render the coupon consumed. My problem is that my update_coupon function is not updating the column as I expect. The first time it worked, but then I must have changed something and subsequent queries no longer work.
Sorry for the noob question. Thank you for any help.
function is_valid_coupon($card_code){
global $db;
$code = sha1($card_code);
$query = 'SELECT * FROM giftcards where card_code = :code';
$statement = $db->prepare($query);
$statement->bindValue(':code', $code);
$statement->execute();
$valid = ($statement->rowCount() == 1);
$statement->closeCursor();
return $valid;
}
function update_coupon($card_code){
global $db;
$code = sha1($card_code);
$query = "
UPDATE `giftcards`
SET taken = used
WHERE card_code = :card_code";
$statement = $db->prepare($query);
$statement->bindValue(':card_code', $code);
$statement->execute();
$statement->closeCursor();
}
Which are executed like so:
if ($card_code){
if (is_valid_coupon($card_code)){
update_coupon($card_code);
}
}
SET taken = useddo you mean to set it to a value of used - which should beSET taken = 'used'with quotes?is_valid_coupon()but at no point do you also check if the coupon has already been used, so in theory you can keep on using the same coupon - if that matters at all.