title: How to INSERT new row and UPDATE old row value?
body: <p>I want to insert a new row to <em>table</em> with my PHP form input values</p>
  
  <p><code>$_POST['bill_amount']</code>, <code>$_POST['paid_amount']</code> and <code>$_POST['due_amount']</code>  </p>
  
  <p>as well as update the <em>due_amount</em> column of the second last row with value "0".</p>
  
  <p>Afterwards, I'd like to select and <em>echo</em> the values of the last row (the newly inserted row).</p>
  
  <p>My PHP code:</p>
  
  <pre><code>&lt;?php session_start(); ?&gt;
  &lt;?php error_reporting(E_ALL ^ E_DEPRECATED); ?&gt;
  
  &lt;body&gt;
  
      &lt;?php
          if(isset($_SESSION['username'])) {
      ?&gt;
  
      &lt;?php
          include 'dbconnection.php';
          $ba = $_POST['bill_amount'];
          $pa = $_POST['paid_amount'];
          $da = $_POST['due_amount']
          $sql = "INSERT into table(bill_amount,paid_amount,due_amount)
                values ('$ba','$pa','$da')";
               "UPDATE table SET due_amount='0' ORDER BY bill_id DESC LIMIT 1,1";
  
          if (!mysql_query($sql)) {
              die('Error:' . mysql_error());
          }
  
          echo "Here's the newly inserted record:";
          $result = mysql_query("SELECT * FROM table ORDER BY bill_id DESC LIMIT 1");  
  
          echo "&lt;table&gt;
                  &lt;tr&gt;
                     &lt;th&gt;Bill ID&lt;/th&gt;
                     &lt;th&gt;Bill Amount&lt;/th&gt;
                     &lt;th&gt;Paid Amount&lt;/th&gt;    
                     &lt;th&gt;Due Amount&lt;/th&gt;
                  &lt;/tr&gt;";
  
          while($row = mysql_fetch_array($result)) {
  
              echo "&lt;tr&gt;";
              echo "&lt;td&gt;" . $row['bill_id'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $row['bill_amount'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $row['paid_amount'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $row['due_amount'] . "&lt;/td&gt;";
              echo "&lt;/tr&gt;";
          }
          echo "&lt;/table&gt;";
      ?&gt;
  
      &lt;HTML&gt;
  
      &lt;?php
          }
          else {
              echo "session expired";
          }
      ?&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?PHP
      mysql_close($con);
  ?&gt;
  </code></pre>
  
  <p>The problem with my PHP code is that it successfully inserts a new row to <em>table</em>, but doesn't update the <em>due_amount</em> column of the second last row with the value "0" </p>
  
  <p>The first view of <em>table</em> :<br>
  (please note that "bill_id" is <code>auto increment primary key</code>) :   </p>
  
  <pre><code>       +------------+-------------+---------------+---------------+  
         | bill_id    | bill_amount | paid_amount   | due_amount    | 
         +------------+-------------+---------------+---------------+
         | 1          | 200         | 100           | 100           |
         +------------+-------------+---------------+---------------+
  </code></pre>
  
  <p>The View of <em>table</em> after executing my PHP code:</p>
  
  <pre><code>       +------------+-------------+---------------+---------------+  
         | bill_id    | bill_amount | paid_amount   | due_amount    | 
         +------------+-------------+---------------+---------------+
         | 1          | 200         | 100           | 100           |
         +------------+-------------+---------------+---------------+
         | 2          | 300         | 100           | 200           |
         +------------+-------------+---------------+---------------+
  </code></pre>
  
  <p>The View of <em>table</em> I would like to have after executing my PHP code:</p>
  
  <pre><code>       +------------+-------------+---------------+---------------+  
         | bill_id    | bill_amount | paid_amount   | due_amount    | 
         +------------+-------------+---------------+---------------+
         | 1          | 200         | 100           | 0             |
         +------------+-------------+---------------+---------------+
         | 2          | 300         | 100           | 200           |
         +------------+-------------+---------------+---------------+
  </code></pre>