dongmen1860 2015-08-26 06:59
浏览 15
已采纳

我可以使用PHP在同一个MYSQL表中从两个其他单元格更新一个单元格

i want to update a single row multiple time. when i update it from one cell it update successfully but when i update it from two cell one of them updated while another want is not.

the MYSQL Table Registration is:

CREATE TABLE `registration` (
  `Reg_id` int(11) NOT NULL AUTO_INCREMENT,
  `Arrears` int(10) NOT NULL,
  `Current_Bill` varchar(20) NOT NULL,
  `Current_Surcharge` varchar(20) NOT NULL,
  PRIMARY KEY (`Reg_id`),
  KEY `Reg_id` (`Reg_id`)
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=latin1;

when i update Arrears from Current_Bill and update Current_Bill to 0. it works. but when i update Arrearsfrom Current_Bill and update Current_Bill to 0 and update Arrears from Current_Surcharge and Current_Surcharge to 0 then Current_Surcharge is updating to Arrears and Current_Surcharge updating to 0 and also Current_Bill Updating to 0 but Arrears can not be updated from Current_Bill

The PHP Code Is:

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

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

        while($row = $result->fetch_assoc()) 
        {
            $currentsurcharge = $row['Current_Surcharge'];
            $currentbill = $row['Current_Bill'];
            $arrears = $row['Arrears'];

            $updatearrearsfromcurrentbill = $arrears + $currentbill;
            $updatecurrentbilltozero = 0;
            $updatearrearsfromcurrentsurcharge = $arrears + $currentsurcharge;
            $updatecurrentsurchargetozero = 0;

            if (strtotime(date('Y-m-d')) > strtotime($duedate)) 
            {
            // thid query is not working while the other 
            // Update Arrears From Current Bill when Bill Is Not Pay On Due Date
             $sql_update5 = "UPDATE  `registration` SET `Arrears`=             $updatearrearsfromcurrentbill WHERE `Reg_id` = $regid";
            // Update Current Bill To Zero 
            $sql_update6 = "UPDATE  `registration` SET `Current_Bill`= $updatecurrentbilltozero WHERE `Reg_id` = $regid";
            // Update Arrears From Current Surcharge when Bill is not pay on due date
            $sql_update7 = "UPDATE  `registration` SET `Arrears`= $updatearrearsfromcurrentsurcharge WHERE `Reg_id` = $regid";
            // Update Current Surcharge to Zero
            $sql_update8 = "UPDATE  `registration` SET `Current_Surcharge` = $updatecurrentsurchargetozero WHERE `Reg_id` = $regid";

            mysqli_query($link, $sql_update5);
            mysqli_query($link, $sql_update6);
            mysqli_query($link, $sql_update7);
            mysqli_query($link, $sql_update8);
    }

    }

    ?>
  • 写回答

1条回答 默认 最新

  • dongwan0574 2015-08-26 07:17
    关注

    You need to specify additional parameters in the where clauses to achieve what you want because currently they ALL simply user where Reg_id=$regid - so in essence you overwrite the arrears data with the second query. In both cases you say, in your comments, that the query should update the arrears if the bill is not paid by due date - that field can only hold one value.

    Effectively, the way it is written currently, your sql is the same as:-

    $sql_effective="update `registration` set 
        `arrears`='$updatearrearsfromcurrentsurcharge',
        `current_bill`='$updatecurrentbilltozero',
        `current_surcharge`='$updatecurrentsurchargetozero'
        where `reg_id`='$regid';";
    

    You need to clarify your logic on what you want to achieve under certain conditions - I'm not clear on what your requirements are.

    Based on your further comments, this might suffice. ( Note that there is only 1 update query! ):

    $sql_update = "update `registration` set 
        `arrears`=".abs( $arrears + $currentbill + $currentsurcharge ).",
        `current_bill`=0,
        `current_surcharge`=0
        where `reg_id`='$regid';";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题