title: how can i make a calculation before inserting the data into a datadase using php mysqli
body: <p>I want to calculate the bond balance using amount saved in the database and bond amount inputted on a form then insert the result to the database plus the other information. am using php and mysql.
  below is the code I have</p>
  
  <pre><code>if (isset($_POST['submit'])) {
      $ref_no = mysqli_real_escape_string($dbc, $_POST['ref_no']);
      $cargo_des = mysqli_real_escape_string($dbc, $_POST['cargo_des']);
      $te_no = mysqli_real_escape_string($dbc, $_POST['te_no']);
      $bond = mysqli_real_escape_string($dbc, $_POST['bond']);
      $bond_no = mysqli_real_escape_string($dbc, $_POST['bond_no']);
      $bond_amount = mysqli_real_escape_string($dbc, $_POST['bond_amount']);
      $date_lodged = mysqli_real_escape_string($dbc, $_POST['date_lodged']);
      //$bond_balance = mysqli_real_escape_string($dbc, $_POST['bond_balance']);
      $query = mysqli_query ($dbc, "SELECT amount FROM bond");
      while( $row = mysqli_fetch_array($query) ) {
          echo $row['amount'];
      }
      $bond_balance = 0;
      $examout = $row['amount'];
      $amount = $_POST['bond_amount'];
      $bond_balance = $examout - $amount;
      $q = "INSERT INTO new_bond (ref_no, cargo_des, te_no, bond, bond_no, bond_amount, date_lodged) "
              . "VALUES ('$ref_no','$cargo_des','$te_no','$bond','$bond_no','$bond_amount','$date_lodged','$bond_balance')";
      $r = mysqli_query($dbc, $q) or die(mysqli_error($dbc));
      if ($r) {
          echo '&lt;script&gt;alert("Success")&lt;/script&gt;';
          echo "&lt;script&gt;window.open('?page=new_bond','_self')&lt;/script&gt;";
      }
      exit();
  }
  </code></pre>