dongteng0748
2017-02-14 11:52
浏览 73
已采纳

如何使用php mysqli将数据插入数据库之前进行计算

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

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 '<script>alert("Success")</script>';
        echo "<script>window.open('?page=new_bond','_self')</script>";
    }
    exit();
}

图片转代码服务由CSDN问答提供 功能建议

我想使用数据库中保存的金额和表格上输入的债券金额来计算债券余额然后插入结果 到数据库加上其他信息。 我正在使用php和mysql。 下面是我的代码

  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,t  e_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)或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(); 
} 
   \  n 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题