【问题标题】:Strange PHP DIE() behaviour奇怪的 PHP DIE() 行为
【发布时间】:2021-11-21 07:20:13
【问题描述】:

我有一个数据库类,它使用 db->query() 和 $db->close 方法 查询类支持准备好的语句,带有?参数与值数组 ($data) 进行比较。

该表有一个 TEXT 数据字段,我用来在其中存储一个大文本字符串,还有一个名为 UPDATED 的列,它在插入查询中使用 NOW() 来显示记录何时被修改。

如果我按标准运行脚本,则 TEXT 数据字段无法更新,但 UPDATED 列会。如果我在 $db->query() 方法之前和之后 var_dump() 文本字段的变量,则根据接收到的 $_POST 数据,这些值是正确的

这就是关键

为了在屏幕上查看输出,我在 $db->query 方法之后添加了 DIE。当我这样做时,TEXT 数据字段和 UPDATE 都成功更新。

public function save(){
    try {
        $this->products = ( !empty( $this->products ) ? servicing_package::packageWriter( servicing_package::packageReader( $this->products ) ) : null );
        var_dump( $this->products );

        $this->job_created_by = ( !empty( $this->job_created_by ) ? $this->job_created_by : session::getTechnicianId() ); 
        $this->job_current_user = ( !empty( $this->job_current_user ) ? $this->job_current_user : ( !empty( $this->job_technician ) ? $this->job_technician : null ) );
        $data = array(
            $this->entity_id,
            $this->customer_id,
            $this->products,
            $this->action,
            $this->job_created_by,
            $this->job_technician,
            $this->job_current_user,
            $this->job_company,
            $this->status,
            $this->location,
            $this->warranty,
            $this->color,
            $this->invoice_quote,
            $this->invoice_total,
            $this->approved_cost,
            $this->approved_technician,
            $this->approved_date,
            $this->completed,
            $this->collected,
            $this->customer_id,
            $this->products,
            $this->action,
            $this->job_created_by,
            $this->job_technician,
            $this->job_current_user,
            $this->job_company,
            $this->status,
            $this->location,
            $this->warranty,
            $this->color,
            $this->invoice_quote,
            $this->invoice_total,
            $this->approved_cost,
            $this->approved_technician,
            $this->approved_date,
            $this->created,
            $this->completed,
            $this->collected,
        );
        $db = new database();
        $db->query("INSERT INTO `". $db->getdbname() ."`.`servicing__job`(`entity_id`, `customer_id`, `products`, `action`, `job_created_by`, 
                `job_technician`, `job_current_user`, `job_company`, 
                `status`, `location`, `warranty`,`color`, `invoice_quote`, 
                `invoice_total`, `approved_cost`, `approved_technician`, 
                `approved_date`, `created`, `updated`, `completed`, 
                `collected`) 
        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),?,?) 
            ON DUPLICATE KEY UPDATE `entity_id` = LAST_INSERT_ID(`entity_id`), 
                    `customer_id` = ?, `products` = ?, `action` = ?, 
                    `job_created_by` = ?, `job_technician` = ?, 
                    `job_current_user` = ?, `job_company` = ?, `status` = ?, 
                    `location` = ?, `warranty` = ?, `color` = ?, 
                    `invoice_quote` = ?, `invoice_total` = ?, 
                    `approved_cost` = ?, `approved_technician` = ?, 
                    `approved_date` = ?, `created` = ?, `updated` = NOW(), 
                    `completed` = ?, `collected` = ?", $data );

die;
        /* If the job is new, record who created it */
        if( empty( $this->entity_id ) ) {
            $_technicians = servicing_controls::getTechnicians();
            foreach( $_technicians as $key => $value ) {
                $technicians[ $value['technician_id'] ] = $value['display_name'];
            }
            $user = ( isset( $technicians[ session::getTechnicianId() ] ) ? $technicians[ session::getTechnicianId() ] : "Removed User" );
            $note = "Job ". $db->lastid() ." created by user ". $user;
            servicing_note::addNote( $db->lastid(), $note, $visibility = 0 );
        }

        $this->entity_id = $db->lastid();
        $db->close();
        return $this;
    }
    catch(Exception $e) {    
        echo "Message : " . $e->getMessage();
    }
}

【问题讨论】:

  • 你到底用什么来执行查询? PDO、MySQLi、某个库?如果有 die() 则更新,您的意思是当您添加 die() 时,数据库会使用新数据更新,如果没有 die(),则仅更新更新的列?

标签: php mysql die


【解决方案1】:

我使用一个自定义类来处理数据库连接,我遇到的问题是当在保存例程中调用 DIE 时,所有记录都会成功更新,但实际问题是数据都在更新情况下,但其中一个初始执行例程从数据库加载数据,除非调用 DIE,否则会将更改的数据顶部保存为另一个函数的一部分。

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 2013-08-09
    • 2018-05-20
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多