如果Condition相同,那么如何在php

时间:2015-08-25 11:47:43

标签: php mysql

我希望在条件相同时更新多个查询。它仅适用于一个查询,并且在不更新的情况下跳过剩余查询。谁能告诉我它是如何工作的?  php代码是:

<?php
include("Database/connection.php");

$sql = "SELECT *  FROM registration,billing_month";
$result = $link->query($sql); 

    while($row = $result->fetch_assoc()) 
    {
        $regid = $row['Reg_id'];
        $billid = $row['Bill_id'];
        $issuedate = $row['Bill_issue_date'];
        $duedate = $row['Bill_due_date'];
        $currentdues = $row['Current_Dues'];
        $currentbill = $row['Current_Bill'];
        $arrears = $row['Arrears'];
        $surcharge = $row['Surcharge'];
        $currentsurcharge = $row['Current_Surcharge'];

        $updatesurcharge = $currentsurcharge + $surcharge;
        $updatedsurcharge = 0;
        $updatebill = $currentbill + $currentdues;
        $updatecurrentbill = 0;
        $updatearrears = $arrears + $currentbill;
        $updatedarrears = $arrears + $currentsurcharge;
        $updatearrearsfrombill = 0;
        $updatearrearsfromsurcharge = 0;
        $addsurchargewitharrears = $arrears + $currentsurcharge;

if (strtotime(date('Y-m-d')) >= strtotime($issuedate)) 
{

  $sql_update = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid";

$sql_update = "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid";

$sql_update = "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid";

$sql_update = "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid";

  mysqli_query($link, $sql_update);
}
}
?>

7 个答案:

答案 0 :(得分:1)

您反复覆盖sql_update变量,因此只执行最后一次查询。

这确实是你想要做的(每次你为它分配一个新的查询后执行查询):

if (strtotime(date('Y-m-d')) >= strtotime($issuedate)) 
{

  $sql_update = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid";

  mysqli_query($link, $sql_update);

  $sql_update = "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid";

  mysqli_query($link, $sql_update);    

  $sql_update = "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid";

  mysqli_query($link, $sql_update);

  $sql_update = "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid";

  mysqli_query($link, $sql_update);
}

答案 1 :(得分:1)

你必须使用

if (strtotime(date('Y-m-d')) >= strtotime($issuedate)) 
{

  $sql_update1 = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid";

$sql_update2 = "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid";

$sql_update3 = "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid";

$sql_update4 = "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid";

  mysqli_query($link, $sql_update1);
  mysqli_query($link, $sql_update2);
  mysqli_query($link, $sql_update3);
  mysqli_query($link, $sql_update4);
}

答案 2 :(得分:1)

mysqli_query($link, $sql_update);

执行您的查询,并覆盖$ sql_update变量。像这样执行每个查询:

  $sql_update = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid";
  mysqli_query($link, $sql_update);
$sql_update = "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid";
  mysqli_query($link, $sql_update);
$sql_update = "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid";
  mysqli_query($link, $sql_update);
$sql_update = "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid";

  mysqli_query($link, $sql_update);

答案 3 :(得分:1)

为每个查询指定一个不同的名称,然后为execute每个人提供一个名称。

    if (strtotime(date('Y-m-d')) >= strtotime($issuedate)) 
    {        
      $sql_upd = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid";
      $sql_upd2 = "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid";
      $sql_upd3 = "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid";
      $sql_upd4 = "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid";

      mysqli_query($link, $sql_upd);
      mysqli_query($link, $sql_upd2);
      mysqli_query($link, $sql_upd3);
      mysqli_query($link, $sql_upd4);
    }

答案 4 :(得分:1)

在每个;结束时添加Query Statement,然后连接variables

实施例

$sql_update = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid;";
$sql_update .= "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid;";
$sql_update .= "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid;";
$sql_update .= "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid;";

    mysqli_query($link, $sql_update);
  

或者

您可以重命名$sql_update1$sql_update2等变量,然后逐个调用它们。

示例: -

$sql_update1 = "UPDATE  `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid";
$sql_update2 = "UPDATE  `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid";
$sql_update3 = "UPDATE  `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid";
$sql_update4 = "UPDATE  `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid";

    mysqli_query($link, $sql_update1);
    mysqli_query($link, $sql_update2);
    mysqli_query($link, $sql_update3);
    mysqli_query($link, $sql_update4);

答案 5 :(得分:1)

请参阅以下链接:

mysqli_multi_query

答案 6 :(得分:0)

您可以多次执行查询

// Copies remaining files to places other tasks can use
copy: {
  dist: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= yeoman.app %>',
      dest: '<%= yeoman.dist %>',
      src: [
        '*.{ico,png,txt}',
        '.htaccess',
        '*.html',
        'images/{,*/}*.{webp}',
        'styles/fonts/{,*/}*.*'
      ]
    }, {
      expand: true,
      cwd: '.tmp/images',
      dest: '<%= yeoman.dist %>/images',
      src: ['generated/*']
    }, {
      expand: true,
      cwd: 'bower_components/bootstrap/dist',
      src: 'fonts/*',
      dest: '<%= yeoman.dist %>'
    },
    **{
      expand: true,
      cwd: 'bower_components/roboto-fontface/',
      src: 'fonts/*',
      dest: '<%= yeoman.dist %>/styles/'
    }**]
  },
  styles: {
    expand: true,
    cwd: '<%= yeoman.app %>/styles',
    dest: '.tmp/styles/',
    src: '{,*/}*.css'
  }
},