0

在“new_payment”表和“new_reservation”表中,每一行(付款)都有一个 res_id(new_reservation 中的 PK 和 new_payment、CASCADE、CASCADE 中的 reser、FK)。我必须填写 3 个输入框。

总账单:[ ]

以前支付: [ ]

到期金额: [ ]

当用户输入房间号并提交时,控制器调用'search_by_room_number'模型,然后模型,

  1. 从 new_reservation 表返回“总计”
  2. 将 new_reservation 表中的“res_id”返回到名为 $res_id 的变量中
  3. 使用该 $res_id,我在 new_payment 表中查找以前的“paid_amount”
  4. ...和 ​​new_payment 表中的“due_amount”

但出乎意料的是,出现了以下错误:

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: models/payments_model.php
Line Number: 16

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/payments_model.php
Line Number: 18

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: models/payments_model.php
Line Number: 16

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/payments_model.php
Line Number: 18

模型:

function search_by_room_number($room_number) {

    $query = $this->db->query("SELECT total FROM new_reservation WHERE room_number = '$room_number' AND payment_status = 'Pending' LIMIT 1"); // Line 16
    $row = $query->row();
    return $row->total; // Line 18

    $res_id='';

    $query = $this->db->query("SELECT res_id FROM new_reservation WHERE room_number = '$room_number' AND payment_status = 'Pending' LIMIT 1");
    $row = $query->row();
    $res_id = $row->res_id;

    $query = $this->db->query("SELECT paid_amount FROM new_payment WHERE res_id = '$res_id' LIMIT 1");
    $row = $query->row();
    return $row->paid_amount;

    $query = $this->db->query("SELECT due_amount FROM new_payment WHERE res_id = '$res_id' LIMIT 1");
    $row = $query->row();
    return $row->due_amount;

}

控制器:

function search_room_number() {
        $popRoomNumbers = $this->input->post('room_number');

        $totalBill['total'] = $this->payments_model->search_by_room_number($popRoomNumbers);
        $previouslyPaid['paid_amount'] = $this->payments_model->search_by_room_number($popRoomNumbers);
        $dueAmount['due_amount'] = $this->payments_model->search_by_room_number($popRoomNumbers);

        $tmpFormData=array_merge($totalBill, $previouslyPaid, $dueAmount);
    $this->load->view('/main/new_payment', $tmpFormData);
}

看法:

<?php $attr = array(
                'name'  =>  'totalBill',
                'id'    =>  'totalBill',
                'class' =>  'span2',
                'readonly'=>'true',
                'value' =>  $total
            );
echo form_input($attr);
?>            

<?php $attr = array(
                'name'  =>  'previouslyPaid',
                'id'    =>  'previouslyPaid',
                'class' =>  'span2',
                'readonly'=>'true',
                'value' =>  $paid_amount
            );
echo form_input($attr);
?> 

<?php $attr = array(
                'name'  =>  'dueAmount',
                'id'    =>  'dueAmount',
                'class' =>  'span2',
                'readonly'=>'true',
                'value' =>  $due_amount
            );
echo form_input($attr);
?>   
4

2 回答 2

3

我认为你应该这样尝试:

$query = $this->db->query("SELECT total, res_id FROM new_reservation WHERE room_number = '".$this->db->escape($room_number)."' AND payment_status = 'Pending' LIMIT 1");

if ($query->num_rows() > 0)
{
    $row = $query->row();
    $total = $row->total;
    $res_id = $row->res_id;

} else {
    throw new Exception("No records found");
}
于 2013-07-09T04:55:22.970 回答
0

我自己又一次又一次地经历了一遍,终于能够提供更有效的代码。

function viewTotalBill($room_number) {

    $query = $this->db->query("SELECT total FROM new_reservation WHERE room_number='$room_number' AND payment_status='Pending' LIMIT 1");
    if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        return $row->total;
        }
    }   
    //else throw new Exception("No records found"); 
}

function viewPreviouslyPaidAmount($room_number) {

    $query = $this->db->query("SELECT res_id FROM new_reservation WHERE room_number='$room_number' AND payment_status='Pending' LIMIT 1");
    if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        $query = $this->db->query("SELECT paid_amount FROM new_payment WHERE res_id = '$row->res_id' LIMIT 1");
            foreach ($query->result() as $row) {
            return $row->paid_amount;
            }
        }
    }
    //else throw new Exception("No records found");
}


function viewDueAmount($room_number) {

    $query = $this->db->query("SELECT res_id FROM new_reservation WHERE room_number='$room_number' AND payment_status='Pending' LIMIT 1");
    if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        $query = $this->db->query("SELECT due_amount FROM new_payment WHERE res_id = '$row->res_id' LIMIT 1");
            foreach ($query->result() as $row) {
            return $row->due_amount;
            }
        }
    }
    //else throw new Exception("No records found");
}
于 2013-07-09T05:44:59.860 回答