如果没有返回,则我需要知道如何检查提交的礼品卡代码是否存在MYSQL:该代码不存在!这是我的代码:
我正在运行PHP 7.2和最新的MySQL
<?php
$userID = $_GET['user'];
$code = $_GET['code'];
if (empty($code) || empty($userID)){
die(error('Fill in all fields'));
}
if ($user -> safeString($code) || $user -> safeString($code)){
die(error('Unsafe characters were set'));
}
$SQL = $odb -> prepare("SELECT `claimedby` FROM `giftcards` WHERE `code` = :code");
$SQL -> execute(array(':code' => $code));
$status = $SQL -> fetchColumn(0);
if (!($status == 0)){
die(error('Gift code has already been claimed!'));
}
// Update Status of GC
$SQLUpdate = $odb -> prepare("UPDATE `giftcards` SET `claimedBy` = :userID, `dateClaimed` = UNIX_TIMESTAMP() WHERE `code` = :code");
$SQLUpdate -> execute(array(':userID' => $userID, ':code' => $code));
// Update User Account with new Plan
$SQL = $odb -> prepare("SELECT `planID` FROM `giftcards` WHERE `code` = :code");
$SQL -> execute(array(':code' => $code));
$planID = $SQL -> fetchColumn(0);
$SQL = $odb -> prepare("SELECT * FROM `plans` WHERE `ID` = :id");
$SQL -> execute(array(':id' => $planID));
$plan = $SQL -> fetch();
$planName = $plan['name'];
$unit = $plan['unit'];
$length = $plan['length'];
$newExpire = strtotime("+{$length} {$unit}");
$updateSQL = $odb -> prepare("UPDATE `users` SET `membership` = :plan, `expire` = :expire WHERE `ID` = :id");
$updateSQL -> execute(array(':plan' => (int)$planID, ':expire' => $newExpire, ':id' => (int)$userID));
echo success('Gift code has been redeem. Plan ('.$planName.') has been added to your account!');
?>
然后我该如何检查Mysql中是否不存在接收到的$ code,如果返回的情况是不存在错误?
答案 0 :(得分:0)
我会将所有选择组合到一个查询中,这将返回所需的所有内容。我知道这可能被认为是效率低下的,因为如果已经声明了一个代码,那么当不需要一个代码时,您正在做一个LEFT JOIN。
但是我猜在大多数情况下,礼品卡没有得到要求,因此可以节省时间。
如果与下面的内容不一样,SQL应该看起来类似(已经晚了,我不应该在这里):
SELECT
T1.`claimedby`,
T1.`planID`,
T2.`length`,
T2.`unit`,
T2.`name`
FROM
`giftcards` AS T1
LEFT JOIN `plans` AS T2 ON T1.`planID` = T2.`id`
WHERE
T1.`code` = :code;
然后,您仅需要在返回中检查“ claimedby”是否具有值,如果有,则说明该值已被声明。正如另一个用户所说的,不要使用“ die”,返回正确的错误代码和/或消息。
然后您可以进行所需的更新,以便在更新会员表之前在礼品卡表上相应地设置“ claimedby”和“ date”字段。