问题与此功能。代码没有执行

时间:2010-03-15 21:14:02

标签: php

该函数应该更新数据库中的值。

以下是代码:

//Functions
//Function to Update users networth
function update_net($name)
    {
    //Get worth & balance at the time
    $sql_to_get_worth_balance = "SELECT * FROM user WHERE username = '$name'";
    $sql_query = mysql_query($sql_to_get_worth_balance);
    while ($rows = mysql_fetch_assoc($sql_query))
    {
     $worth = $rows['worth'];
     $balance_ = $rows['cash_balance'];
    }
    //Get net_worth  now
    $new_net_worth = $worth + $balance;
    //Update net_worth
    $sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'";
    $sql_worth_query = mysql_query($sql_worth);
    }

在这里使用:

//Get username
$username = $_SESSION['username'];

if (isset($username))
{
  //Update networth
  $update_worth = update_net($username);

4 个答案:

答案 0 :(得分:6)

您可能希望在此查询结尾处使用WHERE子句: -

$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'";

e.g。

$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth' WHERE username = '$name';

答案 1 :(得分:3)

  1. 您忘记了更新查询中的name = $ name部分(将更新整个表格!)
  2. 我希望你的$ name永远不能保存用户输入的数据,因为你的sql很容易被注入。

答案 2 :(得分:1)

也许:

//Update net_worth
$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'";
$sql_worth_query = mysql_query($sql_worth);

应阅读:

//Update net_worth
$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'";
$sql_worth_query = mysql_query($sql_for_new_worth);

答案 3 :(得分:0)

可能你应该提交交易吗?