可能重复:
mysql kill process is user dont got enought points PHP
我有一个带有用户系统的积分系统,人们可以用50分升级到[PRO]用户。当他们自动注册时,将[user]写入我的权限字段。但现在我想要一个动作后检查它们是否得到50分然后用[PRO]替换[user]然后减去50分。
<?php
session_start();
//=============Configuring Server and Database=======
$host = 'localhost';
$user = 'root';
$password = '';
//=============Data Base Information=================
$database = 'login';
$conn = mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');
//===============End Server Configuration============
//=============Starting Registration Script==========
$username = mysql_real_escape_string($_POST['txtusername']);
//=============To Encrypt Password===================
//============New Variable of Password is Now with an Encrypted Value========
$insert = "UPDATE `users` SET `points` = (`points`-50) WHERE `username` = '".$username."' and points > 50";
mysql_query($insert);
if (mysql_affected_rows() > 1)
{
// other codes
$insert = "UPDATE `users` SET `rights` = (`rights` [PRO]) WHERE `username` = '".$username."'";
mysql_query($insert);
header('location: succesupgrade.php');
}else{
echo "You don't have enough points to buy [PRO]";
}
?>
再次:我想在人们购买[专业]后,[用户]被[专业]取代。然后他们输了50分。如果他们没有得到50分。 echo说些什么。
答案 0 :(得分:1)
一起做:
UPDATE `users`
SET `rights` = '[PRO]', points = points - 50
WHERE
`username` = 'somename'
AND points >= 50
AND rights != '[PRO]'
然后检查您是否有受影响的行。如果他们没有足够的分数,它就不会更新,当他们已经拥有权利时也不会更新。这可以避免竞争条件。