Home > Mobile >  Update MySQL table with variable for every row by id
Update MySQL table with variable for every row by id

Time:03-04

I have a database, called "MY_Database".

enter image description here

I want to update the "percentage" rows on every count change. Thats how my function looks like so far:

UPDATED AGAIN

            <?php 
            $total = '';
            $result= mysqli_query($conn, "SELECT SUM(count) FROM My_Databse WHERE is_enabled ='1'");
            while($row = mysqli_fetch_array($result)){
                $total = $row['SUM(count)'];
            }
            
            $percentage = '';
            $result= mysqli_query($conn, "SELECT * FROM My_Database WHERE is_enabled ='1' ORDER BY count DESC");
            while($row = mysqli_fetch_array($result)){
                $percentage = ($row[2] / $total) * 100;
                echo '<div >';
                echo '<div  role="progressbar" aria-valuenow="'.$percentage.'" aria-valuemin="0" aria-valuemax="100" style="width:'.$percentage.'%">';
                echo $row[1].'('.round($percentage).')';
                echo '</div>';
                echo '</div>';
                $i  ;
            }
            $result = mysqli_query($conn, "UPDATE My_Database SET percentage = ".$percentage." WHERE id = 1");
            ?>

I have now the problem, that I always get the last percentage. How can I update the percentage for every row?

CodePudding user response:

Update query must be within while loop, after calculating percentage -

mysqli_query($conn, "UPDATE My_Database SET percentage = ".$percentage." WHERE id = ". $row['id'] );
  • Related