没有得到正确的价值作为回报

时间:2016-11-15 13:42:28

标签: php mysql

该任务的想法是允许用户添加和提取" money"来往他们的帐户。问题是我可以加钱,但我不能撤回

$funds = $_POST['funds'];
$withdraw_or_add = $_POST['list'];

if($withdraw_or_add == "add")  
{
  $sql = "UPDATE users SET userFunds = '".$funds."' WHERE userId = 1";
}   
else  
{
  $info = mysql_query("SELECT * FROM users WHERE userId = '1'");
  $info = mysql_fetch_assoc($info);
  $new_fund = $info['userFunds'] - $funds;
  $sql = "UPDATE users SET userFunds = '".$new_fund."' WHERE userId = 1";  
}


mysql_select_db('details_db');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
    die('Could not update data: ' . mysql_error());
}

echo "Updated data successfully\n";

mysql_close($conn);

例如,让我们说$fund = 5$info['userFunds'] = 20,然后变量$new_fund应为15。但相反,它等于-5。如果有人可以提供帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先,您使用了与db连接相关的代码:

$conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('details_db');

然后在mysql_select_db('details_db');

之后显示并删除mysql_
$funds = $_POST['funds'];
$withdraw_or_add = $_POST['list'];

if($withdraw_or_add == "add")  
{
  $sql = "UPDATE users SET userFunds = '".$funds."' WHERE userId = 1";
}   
else  
{
  $info = mysql_query("SELECT * FROM users WHERE userId = '1'");
  $info = mysql_fetch_assoc($info);
  $new_fund = $info['userFunds'] - $funds;
  $sql = "UPDATE users SET userFunds = '".$new_fund."' WHERE userId = 1";  
}


//mysql_select_db('details_db');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
    die('Could not update data: ' . mysql_error());
}

echo "Updated data successfully\n";

mysql_close($conn);

注意:请停止使用mysql_*个功能。在PHP 7中删除了mysql_* extensions。请使用PDOMySQLi