tags:

views:

74

answers:

4

The function is supposed to update the values in the database.

Here is the code:

//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);
    }

It is used here:

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

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

May be you should commit transaction?

Dmitry
+6  A: 

You probably want a WHERE clause on the end of this query:-

$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';
Gavin Gilmour
+3  A: 
  1. You're forgetting the where name=$name part in the update query (which will update the entire table!)
  2. I hope your $name can never hold user entered data because your sql is vulnarable to injection.
Kris
+1  A: 

Maybe:

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

Should Read:

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