-2

下面的代码就像当用户点击提交时,他得到一张票(lodd)并失去五分,因为他用五分买了一张票。但是我很怀念当一个用户没有得到他买不到的五分的时候。我希望如果用户没有获得足够的积分,那么回声会说(示例):

抱歉,您至少需要 5 分才能买票。

我怎样才能做到这一点?现在人们只有在购买比他们拥有的更多时才会减负。

<?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 the 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`-5) WHERE `username` = '".$username."'";
    mysql_query($insert);

    // Other code

    $insert = "UPDATE `users` SET `lodd` = (`lodd` +1) WHERE `username` = '".$username."'";
    mysql_query($insert);

    mysql_query($insert);

    header('location: succes.php');
?>

这是给人们一张五分票的代码:

$insert = "UPDATE `users` SET `points` = (`points`-5) WHERE `username` = '".$username."'";
mysql_query($insert);

// Other code

$insert = "UPDATE `users` SET `lodd` = (`lodd` +1) WHERE `username` = '".$username."'";
mysql_query($insert);

mysql_query($insert);
4

1 回答 1

3

首先,不推荐使用 mysql_* ,因此您应该考虑在某个时候更改它们。一种选择是使用:MySQLi

回到你的问题,你可以这样做:

$insert = "UPDATE `users` SET `points` = (`points`-5) WHERE `username` = '".$username."' and points > 5";
mysql_query($insert);
if (mysql_affected_rows() > 0)
{
    // other codes

    $insert = "UPDATE `users` SET `lodd` = (`lodd` +1) WHERE `username` = '".$username."'";
    mysql_query($insert); 
}else{
    echo "You don't have enough points";
}
于 2012-12-12T16:35:20.193 回答