dougu4704 2013-03-24 01:00
浏览 29
已采纳

如何使用最后一行值来计算在MySQL或php中创建类似值的事务

New to MySQL/php but I am using xampp and trying to create a simple webpage to act like ledger (like checking account kind of deal). So I have debit, gain and balance columns (and some others) in a table which I am inserting a new record:

html

<form id='chk-trans-form' action="example-api.php" method="post">
Detail: <input type='text' name='trans-detail'/><br/>
Date: <input type='text' id='trans-dts'/><br/>
Amount: <input type='text' id='trans-amount'/>
Debit <input type='radio' name='trans-type-group' value='debit' checked/> Gain <input type='radio' name='trans-type-group' value='gain'/>
<button id='submit-trans'>submit</button>
</form>

database.php

<?php
   $conn = mysql_connect('localhost', 'root', '');
   $db   = mysql_select_db('financial');
?>

example-api.php

<?php
    include_once('database.php');

    $transDate = $_POST['trans-dts'];
    $transDetail = $_POST['trans-detail'];
  $transAmount = $_POST['trans-amount'];
  $transType = $_POST['trans-type-group'];

  if ($transType == 'debit')
  {
      if(mysql_query("INSERT INTO chktrans (chkDTS, chkDetail, chkDebit) VALUES ('$transDate','$transDetail', '$transAmount')"))
          echo "Successfully Added";
      else
          echo "Insertion Failed"; 
   } else {
    if(mysql_query("INSERT INTO chktrans (chkDTS, chkDetail, chkGain) VALUES ('$transDate','$transDetail', '$transAmount')"))
        echo $transType;
        else
            echo "Insertion Failed";    
   }
?>

so this was the approach i learned and it works well for getting the data to the table, though I am not sure if its the best approach, just what I found.

So the next part I am a little unsure about....since this is a transaction like table, I need something like Last Balance + (Debit + Gain) = new balance. So I am not sure if this is something that I should be trying to get in php, or do in mysql....I have found triggers for a calculated-like field approach (here), but I am not sure if that will work because I don't know how to get the last value, or if the best approach is creating triggers for this, or trying to pull the value and use php to get the value, calculate and send everything back??

any help or direction is appreciated as always.

  • 写回答

2条回答 默认 最新

  • doukui2011 2013-03-24 01:09
    关注

    Just SELECT the column that you want, store that in a variable, get the other. In this case you'll be getting the 'chkGain' and the necessities for the debit. The last balance will remain the same until the statement is finished invocation.

    $currBalance = $currBalance + $debit + $gain;
    

    Note that in your equation, 'Last Balance + (Debit + Gain),' parentheses aren't needed. Order of operations, my friend.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码