Question : UPDATE command

I have a script that works except for the UPDATE command:

$query = "UPDATE userreg SET networth = '$calculate_balance' WHERE id='$id'";
      mysql_query($query);

Can someone please let me know what I am doing wrong here?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
if ($_POST['sendRequest'] == "savetodb") {
// Access the value of the dynamic text field variable sent from flash
$Full_Name = $_POST['Full_Name'];
$SharesPur = $_POST['purchase_shares'];

include_once "connect_to_mysql.php";
	$sql = mysql_query("SELECT * FROM stock_index WHERE Full_Name='$Full_Name'");
    while($row = mysql_fetch_array($sql)) {
	
	$Industry = $row["Industry"];
	$Symbol = $row["Symbol"];
	$Movement = $row["Movement"];
	$Shares = $row["Shares"];
	$Share_Volume = $row["Share_Volume"];
	$Starting_Value = $row["Starting_Value"];
	$Current_Value = $row["Current_Value"];
	$Total_Cost = $Current_Value * $Shares;

	$sql1 = mysql_query("SELECT * FROM userreg WHERE id='$id'");
    while($row = mysql_fetch_array($sql1)) {
	
	$id = $row["id"];
	$username = $row["username"];
	$networth = $row["networth"];
	
	$calculate_balance = $networth-$Total_Cost;
	$remaining_balance = number_format($calculate_balance);
	
	if ($networth < $Total_Cost) {
	print "errorMsg=success_no";
	} else {
	
	$query = "UPDATE userreg SET networth = '$calculate_balance' WHERE id='$id'";
	mysql_query($query);
	
	$sql2 = mysql_query("INSERT INTO portfolio (username, userid, Full_Name, Symbol, Shares, Bought_At, Starting_Value, Current_Value, Share_Volume, Movement, Industry, open_date) 
     VALUES('$username','$id','$Full_Name','$Symbol','$SharesPur','$Current_Value','$Starting_Value','$Current_Value', '$Share_Volume','$Movement','$Industry', now())")
     or die (mysql_error());

	 }
	 }
	 }
}

Answer : UPDATE command

Your select on line 19 $sql1 = mysql_query("SELECT * FROM userreg WHERE id='$id'");
there is an empty id, so this query returns only rows with id=''. And if there are some lines in the table, then are all updated with your update.
Random Solutions  
 
programming4us programming4us