【问题标题】:Performing conditions on PHP before inserting the data on MySQL database在将数据插入 MySQL 数据库之前在 PHP 上执行条件
【发布时间】:2015-03-07 17:11:21
【问题描述】:

我目前正在处理一个小项目。我将 HTML 用于用户可以输入数据的表单,并使用 PHP 在我的数据库中执行一些查询。我的 PHP 脚本和 SQL 查询的代码有问题。为了说明我在用户单击“保存”按钮时提交表单的简单过程,下面是我想要发生的事情:

但在此之前,我有两个名为ncanca_totals 的表用于保存数据。

表:nca

+--------+---------+------------+------------+--------------+
| nca_id | nca_no  | issue_date | nca_amount | account_type |
+--------+---------+------------+------------+--------------+

表:nca_totals

+----------+-----------+------------+--------------+
| total_id | nca_total | nca_date   | account_type |
+----------+-----------+------------+--------------+

用户点击“保存”按钮后的分步操作:

  1. 将值插入nca 表。例如这里是用户输入表单的值(逗号分隔表示用户的另一个输入):

    nca_id = 1,2,3
    nca_no = 14-0001,14-0002,14-0001
    issue_date = 2015-01-09,2015-01-09,2015-01-10
    nca_amount = 500,1000,2000
    account_type = DBP-TRUST,DBP-TRUST,ROP
    

    我的查询:

    // INSERT to nca table
    $sql3 = "INSERT into nca VALUES ('$nca_id','$nca_no','$nca_date','$nca_amount','$nca_account')";    
    $result3 = mysql_query($sql3);
    

    这是您选择表格时的样子:

    +--------+---------+------------+------------+--------------+
    | nca_id | nca_no  | issue_date | nca_amount | account_type |
    +--------+---------+------------+------------+--------------+
    |      1 | 14-0001 | 2015-01-09 |        500 | DBP-TRUST    |
    |      2 | 14-0002 | 2015-01-09 |       1000 | DBP-TRUST    |
    |      3 | 14-0001 | 2015-01-10 |       2000 | ROP          |
    +--------+---------+------------+------------+--------------+
    
  2. account_type为基础,从表nca中总结出nca_amount

    下面是我的查询:

    // SUM up amount 
    $sql1 = "SELECT SUM(nca_amount) AS nca_total FROM nca WHERE account_type = '$nca_account' AND 
            (year(issue_date) = year('$nca_date') AND
            month(issue_date) = month('$nca_date'))";
    $result1 = mysql_query($sql1);
    while($row1 = mysql_fetch_array($result1)) {
        $db_ncaTotal = $row1['nca_total'];                                                          
    }
    
    // Insert to nca_totals table
    $sql4 = "INSERT into nca_totals VALUES ('','$db_ncaTotal','$nca_date','$nca_account')"; 
    $result4 = mysql_query($sql4);
    

    这是nca_totals表格中的样子:

    +----------+-----------+------------+--------------+
    | total_id | nca_total | nca_date   | account_type |
    +----------+-----------+------------+--------------+
    |        1 | 500       | 2015-01-09 | DBP-TRUST    |
    |        2 | 1500      | 2015-01-09 | DBP-TRUST    |
    |        3 | 2000      | 2015-01-10 | ROP          |
    +----------+-----------+------------+--------------+
    

正如上面我的nca_totals 上的结果所示,它保存了nca_amountSUM,具有DBP-TRUST 的account_type 和2015-01-09 的日期。结果是 1500。

下面的插图可以清楚地解释它:

1 | 14-0001 | 2015-01-09 |        500 | DBP-TRUST    |
2 | 14-0002 | 2015-01-09 |       1000 | DBP-TRUST    |

我对上面sql1 变量的查询将导致 500 + 1000 = 1500 并且我想要发生的事情。但正如您在nca_totals 表上看到的,它仍然有我之前计算nca_amount 总和的结果。

这就是我只想在我的nca_totals 表中没有之前从nca 表中计算出的nca_amount 总和。

+----------+-----------+------------+--------------+
| total_id | nca_total | nca_date   | account_type |
+----------+-----------+------------+--------------+
|        1 | 1500      | 2015-01-09 | DBP-TRUST    | ---- Just this result to save
|        2 | 2000      | 2015-01-10 | ROP          |
+----------+-----------+------------+--------------+

我该怎么做?我可以在我的 PHP 或查询中进行调节吗?真的需要帮助。我的代码在这里真的很乱。它没有达到我的预期。在此先感谢

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    使用此代码,您必须在插入前检查 nca_totals 表!如果该 acc_type 和日期数据存在更新,如果不存在插入行!

    $sql_chk = "SELECT nca_total FROM nca_totals WHERE account_type = '$nca_account' AND substring(nca_date,1,7) = substring('$nca_date',1,7)";
    $result_chk = mysql_query($sql_chk);
    
    if (mysql_num_rows($result_chk)==0) { 
      $sql4 = "INSERT into nca_totals VALUES ('','$db_ncaTotal','$nca_date','$nca_account')"; 
      $result4 = mysql_query($sql4);
    }else{
      $sql4 = "update nca_totals set VALUES nca_total='$db_ncaTotal' WHERE account_type = '$nca_account' AND substring(nca_date,1,7) = substring('$nca_date',1,7)"; 
      $result4 = mysql_query($sql4);
    }
    

    只需将此代码替换为此插入代码!

    // Insert to nca_totals table
    $sql4 = "INSERT into nca_totals VALUES ('','$db_ncaTotal','$nca_date','$nca_account')"; 
    $result4 = mysql_query($sql4);
    

    如果你可以对mysql使用mysqli!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多