在MySQL数据库上插入数据之前在PHP上执行条件

时间:2015-01-09 11:28:19

标签: php mysql

我目前正在处理一个小项目。我使用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. 根据nca_amount的相同日期和年份的表格nca总结account_type

    以下是我的查询:

    // 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          |
    +----------+-----------+------------+--------------+
    
  3. 正如我在nca_totals上方的结果所示,它保存了SUM nca_amount account_type的DBP-TRUST和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_amount表中计算的nca总和。

    +----------+-----------+------------+--------------+
    | 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或查询中进行调节吗?真的需要帮助。我的代码在这里真是一团糟。它没有按照我的预期行事。提前致谢

1 个答案:

答案 0 :(得分:2)

使用此代码,您必须在插入之前检查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);

如果你可以使用mysqli对抗mysql!