下面的编码就像用户点击提交一样,他得到一张票(lodd)并且因为他买了一张5分的票而失去了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);
答案 0 :(得分: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";
}