title: Issue with calculating sum of total variable
body: <p>I'm creating an invoice for my project. There are 3 pages.
  I'm a newbie at database and php (having only 1 month to crash through these 2 subjects) so forgive me if my codes/ or database logic is lacking.</p>
  
  <p>For all 3 pages, they are already connected to the database, i took out the file names so that it wont clutter the code blocks.</p>
  
  <p>The first page is the form of the invoice. The user keys in his/her name, then key in the description, amount, quantity and discount variables. Clicking submit goes to the 2nd page.</p>
  
  <p>The 2nd page is more of back end processing of looping the variables if more than one row of input is entered in page1. It then parses all these inputs into a table called invoicesub. It also encode the customer name into the url at the redirect link at the bottom.</p>
  
  <p>The 3rd page is where i want to display all these variables such as customer name, the descriptions, quantity, amount, total, discount from the table but also make a sum of the total variable in the table invoicesub. I also want to re-insert all the variables of the printed variables into another table in the same database called invoicefinal. </p>
  
  <p>First Page (form)</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var count = 0;
  
  
  function addTextArea(){
  count= count+1;
  var div = document.getElementById('name');
  div.innerHTML += "&lt;div&gt; &lt;input type='text' name='name[]' value='' "+"id=name"+count+"&gt; &lt;/div&gt;";
  div.innerHTML += "\n&lt;br /&gt;";
  var div = document.getElementById('quantity');
  div.innerHTML += "&lt;div&gt;&lt;input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"&gt;&lt;/div&gt;";
  div.innerHTML += "\n&lt;br /&gt;";
  var div = document.getElementById('amount');
  div.innerHTML += "&lt;div&gt;&lt;input type='text' name='amount[]'  value ='' "+"id=amount"+count+"&gt;&lt;/div&gt;";
  div.innerHTML += "\n&lt;br /&gt;";
  var div = document.getElementById('discount');
  div.innerHTML += "&lt;div&gt;&lt;input type='text' name='discount[]'  value ='' "+"id=discount"+count+"&gt;&lt;/div&gt;";
  div.innerHTML += "\n&lt;br /&gt;";
  }
  
  function removeTextArea(){
  document.getElementById("name"+count).remove();
  
  document.getElementById("quantity"+count).remove();
  
  document.getElementById("amount"+count).remove();
  
  document.getElementById("discount"+count).remove();
  count = count-1;
  }
  
  &lt;/script&gt;
  
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;form  action="invoicesubmit.php" method="POST"&gt;
  &lt;?php
  echo "&lt;table border='2'&gt;\n"; 
  echo "&lt;tr&gt;\n"; 
  echo "&lt;th&gt;Description&lt;/th&gt;\n"; 
  echo "&lt;th&gt;Quantity&lt;/th&gt;\n"; 
  echo "&lt;th&gt;Amount($)&lt;/th&gt;\n";
  echo "&lt;th&gt;Discount(%)&lt;/th&gt;\n";
  echo "&lt;/tr&gt;"; 
  
  echo "&lt;tr&gt;";
  echo "&lt;td&gt;"?&gt;&lt;input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/&gt;&lt;?php "&lt;/td&gt;";
  echo "&lt;td&gt;"?&gt;&lt;input type='text' size="50" name='quantity[]' value='' /&gt;&lt;?php "&lt;/td&gt;";
  echo "&lt;td&gt;"?&gt;&lt;input type='text' size="50" name='amount[]' value='' /&gt;&lt;?php "&lt;/td&gt;";
  echo "&lt;td&gt;"?&gt;&lt;input type='text' size="50" name='discount[]' value='' /&gt;&lt;?php "&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  
  echo "&lt;tr&gt;"; 
  echo "&lt;td&gt;"?&gt;&lt;div id="name"&gt;&lt;/div&gt; &lt;?php "&lt;/td&gt;"; 
  echo "&lt;td&gt;"?&gt;&lt;div id="quantity"&gt;&lt;/div&gt; &lt;?php "&lt;/td&gt;"; 
  echo "&lt;td&gt;"?&gt;&lt;div id="amount"&gt;&lt;/div&gt; &lt;?php "&lt;/td&gt;"; 
  echo "&lt;td&gt;"?&gt;&lt;div id="discount"&gt;&lt;/div&gt; &lt;?php "&lt;/td&gt;"; 
  echo "&lt;/tr&gt;";
  ?&gt;
  Customer Name:
  &lt;br /&gt;
  &lt;input type="text" name="cust_name" value="" /&gt; &amp;nbsp;
  &lt;br /&gt;
  &lt;input type="button" value="Add Description" onClick="addTextArea();"&gt; &amp;nbsp;
  
  
  &lt;input type="button" value="Remove Description" onClick="removeTextArea();"&gt; &amp;nbsp;
  &lt;input type="submit" name="submit" value="submit"&gt;
  
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Second Page (Inserting variables into table invoicesub)</p>
  
  <pre><code>if (isset($_POST['submit'])){ // Process the form
      $name_array = $_POST['name'];
      $quantity_array = $_POST['quantity'];
      $amount_array = $_POST['amount'];
      $discount_array = $_POST['discount'];
      $cust_name_array = mysql_prep( $_POST['cust_name']);
      for ($i = 0; $i &lt; count($name_array); $i++){
          $cust_name = $cust_name_array;
          $name = $name_array[$i];
          $quantity = $quantity_array[$i];
          $amount = $amount_array[$i];
          $discount = $discount_array[$i];
          $total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
  
  
              global $connection;
              $query = "INSERT INTO invoicesub (";
              $query.= " cust_name, description, quantity, amount, discount, total";
              $query.= ") VALUES (";
              $query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
              $query.= ")";
  
              $result = mysqli_query($connection, $query);
  
  
          }
  
  
             redirect_to("invoicesubmitfinal.php?cname=".urlencode($cust_name));
  
          }
  </code></pre>
  
  <p>3rd page (trying to sum total variable)</p>
  
  <pre><code>&lt;?php
  
  echo "&lt;table border='1'&gt;\n";
  echo "&lt;tr&gt;\n";
  echo "&lt;th&gt;Description&lt;/th&gt;\n";
  echo "&lt;th&gt;Quantity&lt;/th&gt;\n";
  echo "&lt;th&gt;Amount($)&lt;/th&gt;\n";
  echo "&lt;th&gt;Discount(%)&lt;/th&gt;\n";
  echo "&lt;th&gt;Total_amt&lt;/th&gt;\n";
  echo "&lt;/tr&gt;";
  
  
  global $connection;
  
  $sql1="SELECT sum(amount) as amount, sum(quantity) as quantity, sum(total) as total FROM invoicesub where cust_name=$name";
  $result2 = mysqli_query($connection, $sql1) or die(mysqli_error());
  
  while ($rows = mysql_fetch_array($result2)){
              echo "&lt;tr&gt;";
              echo "&lt;td&gt;" . $rows['quantity'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $rows['amount'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $rows['discount']. "%" . "&lt;/td&gt;";
              echo "&lt;td&gt;" ."$". $rows['total']  . "&lt;/td&gt;";
              echo "&lt;td&gt;" . "$" . $total_amt . "&lt;/td&gt;";
              echo "&lt;/tr&gt;";
      echo $rows['amount'];
      echo $rows['quantity'];
      echo $rows['total'];
  
  }
  ?&gt;
  </code></pre>