title: How to make a totals page in php with data from two tables in MYSQL database
body: <p>I have a MYSQL database for which I am trying to develop a php interface for a stock tracking database. The database has the following tables  and fields (simplified). Goods will be received from suppliers and then be issued out to personnel. The database intends to track the goods
  The tables and fields are 
  tblparts (partID, PartName, Category, Decsription)
  tblissues (partID, IssueID, DateIssued, Quantity, IssuedTo)
  tblreceipts (partID, ReceiptID, DateReceived, ReceivedFrom, VoucherNo, Quantity)
  I have need of help and I am a self taught new comer to MYSQL and PHP having moved from a self taught MS Access. Infact this database is working in Access but now it is needed online hence my switching to php
  First I need a table the will summarize the receipts and issues and will be grouped by partID. The code that I have used is this it is however giving false calculations I don’t know what I have done wrong
  Summary.php (the working assumption is that all goods will be received first so there can not be an issuing out without a receiving first, but there can be a receiving without an issuing out or there might also be a part without any received goods or issued goods.  )</p>
  
  <pre><code>    &lt;html&gt;
  &lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" href="mystyle.css" /&gt;
      &lt;title&gt;View Transactions&lt;/title&gt;
  &lt;h1&gt;Transactions&lt;/h1&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php
  
      // connect to the database
      include('connect.php');
  
  
   // query db
  
   $PartID = $_GET['PartID'];
     // get results from database
    $result = mysql_query("SELECT tblparts.PartName, tblparts.PartID, tblreceiving.ReceivingID, tblreceiving.DateReceived, tblreceiving.VoucherNo, SUM(tblreceiving.Quantity), tblreceiving.ReceivedFrom, tblissues.IssueID, SUM(tblissues.Quantity), tblissues.IssueDate, tblissues.IssuedTo
  FROM tblparts
  
  LEFT JOIN tblreceiving
  ON tblparts.PartID=tblreceiving.PartID
  LEFT JOIN tblissues
  ON tblparts.PartID=tblissues.PartID
  
  WHERE tblparts.PartID=$PartID
  GROUP BY tblparts.PartID
  ORDER BY tblreceiving.TransDate") 
             or die(mysql_error());  
  
  
      echo "&lt;p&gt;&lt;b&gt;View All&lt;/b&gt; &lt;/a&gt;&lt;/p&gt;";
  
      echo "&lt;table border='1' cellpadding='10'&gt;";
      echo "&lt;tr&gt; &lt;th&gt;Part Name&lt;/th&gt; &lt;th&gt;Amount Received&lt;/th&gt;&lt;/tr&gt;";
  
      // loop through results of database query, displaying them in the table
      while($row = mysql_fetch_array( $result )) {
  
              // echo out the contents of each row into a table
              echo "&lt;tr&gt;";
  echo '&lt;td&gt;' . $row['PartName'] . '&lt;/td&gt;';
              echo '&lt;td&gt;' . $row['SUM(tblreceiving.Receipts)'] . '&lt;/td&gt;';
              echo '&lt;td&gt;' . $row['SUM(tblissues.QtyIssued)'] . '&lt;/td&gt;';
   echo "&lt;/table&gt;";
  ?&gt;
  
  &lt;p&gt;&lt;a href="home.php"&gt;Home&lt;/a&gt;&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Second is the code to enter received goods as well issued goods? Currently I have the following code which is supposed to insert into receipts table but giving an error I don’t know where I have got it wrong
  The html form for inserting data</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;form action="insert_receipts.php" method="post"&gt;
  Transaction Date: &lt;input type="text" name="DateReceived" /&gt;
  Voucher Number: &lt;input type="text" name="VoucherNo" /&gt;
  Received From: &lt;input type="text" name="ReceivedFrom" /&gt;
  Quantity: &lt;input type="text" name="Quantity" /&gt;
  Unit: &lt;input type="text" name="Unit" /&gt;
  &lt;input type="submit" /&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The insert_receipts.php</p>
  
  <pre><code>&lt;?php
  $con = mysql_connect("localhost","root","");
  if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
  
  mysql_select_db("stockcontrol", $con);
  
  $sql="INSERT INTO tblreceiving (Transaction Date, Voucher Number, Received From, Quantity)
  VALUES
  ('$_POST[DateReceived]','$_POST[VoucherNo]','$_POST[ReceivedFrom]','$_POST[Quantity]')";
  
  if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
  echo "1 record added";
  </code></pre>
  
  <p>mysql_close($con)</p>
  
  <p>I would wish if the insert_receipts.php would also have a running total if that’s not asking too much.</p>