mysql替换字段中的文本php [英] mysql replace text in a field php

查看:163
本文介绍了mysql替换字段中的文本php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

mysql kill进程是用户没有足够的点PHP

我有一个用户系统的积分系统,人们可以用50分升级到[PRO]用户。当他们注册它自动地写[user]到我的权利字段。但现在我想要这样一个动作后,它检查如果他们有50分,然后替换[用户]与[PRO],然后subtrac 50点。

i have a points system with user system where people can upgrade to [PRO] user with 50 points. When they register it automaticly write [user] down to my rights field. but now i want so after a'n action it check if they got 50 points and then replace [user] with [PRO] and then subtrac 50 points.

<?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],[用户] ]。然后他们失去50点。如果他们没有得到50分。

Again: i want so after people buy [PRO], the [user] get replaced with [PRO]. ANd then they lose 50 points. and if they dont got 50 points. echo says something.

推荐答案

在一个中执行:

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

只需检查你是否有一个受影响的行或不。如果他们没有足够的积分,它不会更新,也不会,当他们已经有权利。这避免了竞态条件。

And just check whether you have an effected row or not. If they don't have enough points, it won't update, and neither will it when they already have the right. This avoids race conditions.

这篇关于mysql替换字段中的文本php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆