SQL UPDATE not updating new SUM to database?

Viewed 41

---- It appears that the ----

`"$query_upd = 'UPDATE registry SET net_worth = "'.$new_value.'" WHERE id = "'.$curr_id.'" LIMIT 1;';"` 

is not updating the DB with the new value. All of the other codes work and echoes fine.

<?php
    
    $sql = "SELECT id, net_worth FROM registry WHERE active = '1'";
    $result = $conn->query($sql);
    
    echo '<div class="row" style="margin-top:20px">';
    
    if ($result->num_rows > 0) {
      // output data of each row
      while($row = $result->fetch_assoc()) {
    
                  $new_value = $row['net_worth'] * 1.25;
                  echo $row["net_worth"];
                  echo ' | ';
                  echo $new_value;
                  echo ' ';
                  $curr_id = $row['id'];
                  $query_upd = 'UPDATE registry SET net_worth = "'.$new_value.'" WHERE id = "'.$curr_id.'" LIMIT 1;';
                  $upd = mysqli_query($link, $query_upd); 
                  echo "Record ".$curr_id." Updated";
      
    
                  // ----- Net Worth
                  echo '<p>';
                  echo '<span style="color:#29cc6a">NET WORTH: '.$curr_id.'</span>';
                  echo '<br>';
                  $fmt = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );
                  echo '<span style="margin-top:-20px;font-size:60px;">'.$fmt->formatCurrency($row["net_worth"], "JPY").'</span>';
                  echo '<br>Yuanbao 元寶';
                  echo '</p>';
    
                  echo '<hr style="border-top:1px, solid, #333">';
                
            }
    
          echo '</div>';
    
      } else {
      echo "0 results";
    }
    $conn->close();
    
    ?> 
1 Answers

it seems that you want to multiply by 1.25 the net_worth of the registry table for the rows with value active = 1 to do this you do not need to multiply using php, you can do it directly with sql

    UPDATE registry 
    SET net_worth= net_worth*1.25
    WHERE active = '1'

be sure that active is a string otherwise you should use

   UPDATE registry 
    SET net_worth= net_worth*1.25
    WHERE active = 1
Related