在进入负余额之前存在mysql余额检查金额

时间:2011-12-10 12:44:43

标签: php mysql syntax

嗨,我已经制作了这个简单的脚本,但我需要检查天平是否正确才会变为负数。如何添加一行来执行此操作,以及该行应该包含哪些内容?一个例子真的很好,因为这个特定的功能都没有。

这是代码。有两个文件,因此我总是可以对不同的数量进行编辑。

<php code>


<? require ("tracker.php");

?>


<center>
<?php 
include 'dbc.php';


?>
<title>J~Net Level Up</title>
 <?php session_start();
 $id = $_SESSION['user_id']; 
 // Connects to your Database 
 mysql_connect("localhost", "root", "pass") or die(mysql_error()); 
 mysql_select_db("messages") or die(mysql_error()); 
 $data = mysql_query("UPDATE `users` SET `balance` = `balance` - 1000 WHERE `users`.`id` =$id") 
 or die(mysql_error()); 
 echo "<table border cellpadding=3>"; 
 while($info = mysql_fetch_array( $data )) 
{ 
 echo "<tr>"; 
 echo "<th>User:</th> <td>".$info['user_name'] . "</td> "; 
 echo "<th>Balance:</th> <td>".$info['balance'] . " </td></tr>"; 
 } 
 echo "</table>";
 // echo "Balance Is ".$_SESSION['balance']; 
echo $row['user_name'] . " " . $row['balance'];
 ?>
<META HTTP-EQUIV=REFRESH CONTENT="0; URL=010101levup.php">

<end php code for page 1>

后来做了魔术。第一页用于整理余额,第二页仅在第1页验证余额后继续进行,因此此时并不需要添加。

有什么想法吗?任何人,请尽可能帮助。

1 个答案:

答案 0 :(得分:-1)

这应该按照您的意愿,如果用户平衡&gt;则减去1000; 0 else设置为0

<php code>


<? require ("tracker.php");

?>


<center>
<?php 
    include 'dbc.php';


?>
<title>J~Net Level Up</title>
 <?php session_start();
 $id = $_SESSION['user_id']; 
 // Connects to your Database 
 mysql_connect("localhost", "root", "pass") or die(mysql_error()); 
 mysql_select_db("messages") or die(mysql_error()); 

 $user = mysql_query("SELECT * FROM `users` WHERE `users`.`id` =$id") or die(mysql_error());

 $balance = $user['balance'] > 1000 ? ($balance - 1000) : 0;

 mysql_query("UPDATE `users` SET `balance` = $balance WHERE `users`.`id` =$id") 
 or die(mysql_error()); 
 echo "<table border cellpadding=3>"; 
 //while($info = mysql_fetch_array( $data )) 
 //{ 
     echo "<tr>"; 
     echo "<th>User:</th> <td>".$user['user_name'] . "</td> "; 
     echo "<th>Balance:</th> <td>".$balance . " </td></tr>"; 
 //} 
 echo "</table>";
 // echo "Balance Is ".$balance; 
echo $user['user_name'] . " " . $balance;
 ?>
<META HTTP-EQUIV=REFRESH CONTENT="0; URL=010101levup.php">

<end php code for page 1>