我有我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功能更好吗?银行的价值不为负吗?
最佳答案
您可以使用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]'");
http://dev.mysql.com/doc/refman/5.0/en/if.html
关于php - 一个提交按钮上有两个_POST,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27986772/