duanguzhong5776 2016-11-15 13:42
浏览 27
已采纳

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

The idea of the task is to allow the user to add and withdraw "money" to and from their account. The problem is I can add money, but I can't withdraw it

$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
";

mysql_close($conn);

So for example, let's say $fund = 5 and $info['userFunds'] = 20 then the variable $new_fund should be 15. But instead it equals -5. If anyone can help it would be much appreciated.

  • 写回答

1条回答 默认 最新

  • dqqy64515 2016-11-15 14:10
    关注

    Firstly page of top you put used db connection related code :

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

    and then bellow and removed mysql_select_db('details_db'); line after 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
    ";
    
    mysql_close($conn);
    

    Note: Please stop using mysql_* functions. mysql_* extensions have been removed in PHP 7. Please used PDO and MySQLi.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch