2016-01-24 78 views
0

我已經做了一個PHP的遊戲,其中人們可以升級他們的東西,錢將從他們的帳戶中扣除。但是,當貨幣價值降低時,貨幣價值就會減少,貨幣升級。這裏是代碼:修復金錢價值在減去

<?php 
session_start(); 
include_once 'dbconnect.php'; 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "dbtest"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 

if(!isset($_SESSION['user'])) 
{ 
header("Location: index.php"); 
} 
?> 
</html> 
<head> 
<body bgcolor="black"> 
<?php echo "<font color=\"#49fb35\" size=\"5\">Update Firewall Software $300</font><br>"; ?> 
<a href="firewall.php"><font color="#49fb35" size="5">Upgrade Firewall.exe for $300</font></a> 
</head> 
</html> 

如何在貨幣價值低時停止升級?

回答

2

您可以用PHP來驗證該值大於0,通過使用:

$query1 = "SELECT * from users WHERE user_id=".$_SESSION['user']; 
$result = mysql_query($query1); 

while($row = mysql_fetch_array($result)) { 
    // Get money from column 
    $money = $row['money']; 
} 

if($money >= 300) { 

$query = "UPDATE users SET firewall = firewall + 1, money = money - 300 WHERE user_id=".$_SESSION['user']; 

$update = mysqli_query($conn,$query); 

} else { 

// Prints error 
echo 'Money is not enough to upgrade'; 

} 

我不知道你使用的變量名,就可以相應地改變,希望這有助於!

+0

我不明白你能給我的代碼?我正在使用$ money – BlackHacker

+0

您能提供用於診斷的firewall.php代碼嗎?你從哪裏得到用戶的$錢?謝謝 – Panda

+0

這是firewall.php代碼http://pastebin.com/PLDSLXk3 – BlackHacker