-3

i need to sum two echo values that fetch by data base according to a specific id.i need to sum the value that i get from database sql (id="soil_value_id" and id="land_extent_id" ) it have to be automatically showing in a text box . please help me thank you :)

<?php 
$con = mysqli_connect("localhost","root","","loan_db");
                                  
 {
$stud_id = $_POST['borrower_id']  ;

$query = "SELECT * FROM borrowers WHERE id='$stud_id' ";
$query_run = mysqli_query($con, $query);

if(mysqli_num_rows($query_run) > 0)
{
foreach($query_run as $row)
                                            
{
 ?>

<label for=""style="color: #1b78c9"><b> soil type value  </b></label>
<input type="number" id="soil_value_id" value="<?= $row['soil_value']; ?>" disabled/>
                                              
<label for=""style="color: #1b78c9"><b>  land extent </b></label>
<input type="number" id="land_extent_id" value="<?= $row['land_extent']; ?>" disabled/>
    
</div>                              
Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • One possible way, is to use another variable (maybe `$total`?) to add those two things together, and then use the `$total` with the textbox. – Paul T. Dec 26 '22 at 14:17
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 29 '22 at 17:02
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jan 07 '23 at 14:25

1 Answers1

0

I'm going to try and answer this, but I may be misunderstanding your question...

//Connect to DB stuff
//SELECT from DB stuff

if (mysqli_num_rows($query_run) > 0 {
    foreach($query_run as $row) {
        $sum = $row['soin_value'] + $row['land_extent'];
        echo "<div class='label'>Soil Type Value</div>";
        echo "<div class='value'>{$row['soil_value']}</div>";
        <br>
        echo "<div class='label'>Land Extent</div>";
        echo "<div class='value'>{$row['land_extent']}</div>";
        <br>
        echo "<div class='label'>Total Value</div>";
        echo "<div class='value'>$sum</div>";
        <br><br>
    }
}

Then you can add some CSS to make it look how you want.

jpgerb
  • 1,043
  • 1
  • 9
  • 36