我需要帮助才能让这段交易代码按预期工作。看来我没有正确放置 BEGIN 和 COMMIT 。每当增加/减少金额时,余额应增加/减少,但事实并非如此。
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if(isset($_POST['amount']))
{
if(isset($_POST['ADD']) && $number !="")
{
mysql_query("BEGIN");
$query="UPDATE accounts SET balance=balance+amount WHERE number='12345'";
}
else
{
$query="UPDATE accounts SET balance='balance-amount' WHERE number='12345'";
mysql_query($query);
}
if($query){
mysql_query("COMMIT");
}
else{
mysql_query("ROLLBACK");
}
}
echo <<<_END
<form action='transaction.php' method="post"><pre>
Enter the amount: <input type="text" name="amount" />
<input type="submit" value="ADD VALUE" /> <input type="submit" value="SUBTRACT VALUE" />
</pre></form>
_END;
$query="SELECT balance FROM accounts WHERE number='12345'";
$result=mysql_query($query);
$rows=mysql_num_rows($result);
for($j=0; $j<$rows; ++$j)
{
$row=mysql_fetch_row($result);
echo <<<_END
<pre>
Your current balance is $row[$j]
</pre>
_END;
}
echo <<<_END
<form action="transaction.php" method="post">
<input type="hidden" name="ADD" value="yes" />
<input type="hidden" name="SUBTRACT" value="yes" />
</pre></form>
_END;
mysql_close($db_server);
?>
我究竟做错了什么?