0

I am looking for the right syntax how to sum up a specific variable that I looped. basically, I made a loop. This is the source code.

<?php
            $n = $_POST['n'];
            for ($i=1;$i<=$n;$i++) {
          ?>
            <h3 style="text-align:center"><b>ITEM <?php echo $i?>:</b></h3>
            <p>DESCRIPTION:</p>
            <input type="text" class="form-control" name="<?php echo $i.'description'; ?>" value="" autofocus>
            <p>PROGRAM UNIT:</p>
            <input type="text" class="form-control" name="<?php echo $i.'prog_unit'; ?>" value="" autofocus>
            <p>PROGRAM QUANTITY:</p>
            <input type="number" class="form-control" name="<?php echo $i.'prog_quantity'; ?>" min="0" value="0" step= ".001" autofocus>
            <p>PROGRAM UNIT COST:</p>
            <input type="number" class="form-control" name="<?php echo $i.'prog_unitcost'; ?>" min="0" value="0" step= ".00001" autofocus>
            <p>QUANTITY ACCOMPLISHED PREVIOUS:</p>
            <input type="number" class="form-control" name="<?php echo $i.'q_prev'; ?>" min="0" value="0" step= ".01" autofocus>
            <p>QUANTITY ACCOMPLISHED THIS REPORT:</p>
            <input type="number" class="form-control" name="<?php echo $i.'q_report'; ?>" min="0" value="0" step= ".01" autofocus>
            <p>QUANTITY ACCOMPLISHED TOTAL TO DATE:</p>
            <input type="number" class="form-control" name="<?php echo $i.'q_total_date'; ?>" min="0" value="0" step= ".01" autofocus>
            <p>PERCENT ACCOMPLISHED PREVIOUS:</p>
            <input type="number" class="form-control" name="<?php echo $i.'p_prev'; ?>" min="0" value="0.00" step= ".01" autofocus>
            <p>COST INCURRED:</p>
            <input type="number" class="form-control" name="<?php echo $i.'cost'; ?>" min="0" value="0" step= ".01" autofocus>
            <input type="hidden" name="n" value="<?php echo $n; ?>"><br>
                <?php }?>

This is the output of the code above. enter image description here

And then, this is the syntax where I insert the looped values in to the database.

if(isset($_POST['submit']))
{

  $n = $_POST['n'];
  for($i=1;$i<=$n;$i++)
  {
    $project = $_POST["project"];
    $description = $_POST[$i."description"];
    $prog_unit = $_POST[$i."prog_unit"];
    $prog_quantity = $_POST[$i."prog_quantity"];
    $prog_unitcost = $_POST[$i."prog_unitcost"];
    $prog_total = ($prog_quantity * $prog_unitcost);
    $q_prev = $_POST[$i."q_prev"];
    $q_report = $_POST[$i."q_report"];
    $q_total_date = $_POST[$i."q_total_date"];
    $p_prev = $_POST[$i."p_prev"];
    $p_report = ($q_total_date/$prog_quantity)*100;
    $cost = $_POST[$i."cost"];

    mysqli_query($bd,"insert into detailed_rpapi (project,description,prog_unit,prog_quantity,prog_unitcost,prog_total,q_prev,q_report,q_total_date,p_prev,p_report,cost)
     values ('$project','$description','$prog_unit','$prog_quantity','$prog_unitcost','$prog_total','$q_prev','$q_report','$q_total_date','$p_prev','$p_report','$cost')");

  }
  $sum = $prog_total + $prog_total;
  echo $sum;
  echo "Data Added Successfully ...";
}

I want to sum all the value from $prog_total. Please help me huhuhu

Jadey
  • 49
  • 6
  • **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/5741187) – Dharman Feb 23 '22 at 10:46

1 Answers1

0

It looks like you are close. Instead of $sum = $prog_total + $prog_total; try $sum = $sum + $prog_total;

Read this, https://www.php.net/manual/en/language.operators.assignment.php, and play with the code below.

<?php

$ImEqualtoI = 0;
$ImAlsoEqualtoI = 0;
$runningTotal = 0;
$runningTotal2 = 0;

for ($i=0; $i<10;$i++) {
    $runningTotal = $runningTotal + $i;
    $runningTotal2 += $i;
    
    ++$ImEqualtoI;
    $ImAlsoEqualtoI = $ImAlsoEqualtoI + 1;
}

var_dump($runningTotal, $runningTotal2, $ImEqualtoI, $ImAlsoEqualtoI, $i);

/*
int(45)
int(45)
int(10)
int(10)
int(10)
*/

UPDATE: OK, I saw that one mistake and just assumed you were getting what you expected for $_POST[$i."prog_quantity"]; or $_POST[$i."prog_unitcost"];. Are those the expected values?

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • Hi, I tried $sum = $sum + $prog_total but it is still not working. – Jadey Feb 23 '22 at 03:19
  • Well let's assume you are adding zero. Need to take a step back and make sure you are getting the expected values. Are you even looping the correct number of times? Can you debug with some `var_dump` and `die` statements maybe? – ficuscr Feb 23 '22 at 03:25
  • Yes, I am getting the expected value sir. But my problem is I don't know how to get the total sum under '$prog_total'. – Jadey Feb 23 '22 at 04:36
  • `$sum = $sum + $prog_total;` Needs to be **inside the loop**. After the loop, check the value of `$sum` for what you desire. Sorry, I kept overlooking problems. Hopefully we got there in the end! Suggest playing with my code example, looking at `foreach`, and using better structured multidimensional arrays. Read: https://stackoverflow.com/questions/42347889/input-field-with-variable-name-how-to-get-the-posted-values – ficuscr Feb 23 '22 at 04:39
  • Thank you so much. It is now working. ilysm – Jadey Feb 23 '22 at 05:00