我有我的POST动作的代码,但是很长,有两个不同的动作,如果用户有钱从金库购买,如果用户没有卡升级,代码就像这个:
if (isset($_POST['shop'])) {
if (security($_POST['ccard']) == 1) {
$umoney = $urow['bank'] + $urow['money'];
} else {
$umoney = $urow['money'];
}
mysql_query(set the total upgrade values in another table)
mysql_query(give the user upgrade values in another table)
}
if (security($_POST['ccard']) == 1) {
$upuser = mysql_query("UPDATE `users` SET bank=bank-'$sumtotc' WHERE id='$urow[id]'");
} else {
$upat2 = mysql_query("UPDATE `users` SET money=money-'$sumtotc' WHERE id='$urow[id]'");
}
但如果我这样做,如果用户购买价值超过银行的东西,它将在银行中带负值,我试着用这个:
if ($urow['bank'] < 0) {
$upmon = mysql_query("UPDATE `users` SET money=money-'$urow[bank]' WHERE id='$urow[id]'");
$upban = mysql_query("UPDATE `users` SET bank=0 WHERE id='$urow[id]'");
}
还有另一种方法可以让ccard更好吗?银行的价值不是负面的?
答案 0 :(得分:0)
您可以使用IF语句,如:
$upuser = mysql_query("UPDATE `users`
SET bank =
CASE
WHEN bank-'$sumtotc' <= 0
THEN 0
WHEN bank-'$sumtotc' >= 1
THEN bank-'$sumtotc'
END
money =
CASE
WHEN bank-'$sumtotc' <= 0
THEN money-'$urow[bank]'
WHEN bank-'$sumtotc' >= 1
THEN money-'$sumtotc'
END
WHERE id='$urow[id]'");