【问题标题】:mysql replace text in a field php [duplicate]mysql替换字段php中的文本[重复]
【发布时间】:2012-12-01 17:06:29
【问题描述】:

可能重复:
mysql kill process is user dont got enought points PHP

我有一个带有用户系统的积分系统,人们可以用 50 积分升级到 [PRO] 用户。当他们注册时,会自动将 [user] 写入我的权限字段。但现在我想在采取行动后检查他们是否得到 50 分,然后将 [user] 替换为 [PRO],然后减去 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]";
}


?>

再次:我希望在人们购买 [PRO] 之后,[user] 被 [PRO] 替换。然后他们失去了 50 分。如果他们没有得到 50 分。 echo 说了些什么。

【问题讨论】:

  • @MarcB - 相同的程序,不同的问题。

标签: php mysql database


【解决方案1】:

一次性完成:

UPDATE `users` 
SET `rights` = '[PRO]', points = points - 50
WHERE 
   `username` = 'somename'
   AND points >= 50
   AND rights != '[PRO]'

只需检查您是否有受影响的行。如果他们没有足够的积分,它就不会更新,当他们已经有权利时也不会更新。这样可以避免竞争条件。

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2013-04-05
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多