codeigniter 3.0.1中的数组到字符串转换错误

时间:2022-09-22 16:32:00

I have the following error message while updating my records in database

我在更新数据库中的记录时出现以下错误消息

Severity: Notice

严重性:注意

Message: Array to string conversion

消息:数组到字符串转换

Filename: database/DB_query_builder.php

文件名:database / DB_query_builder.php

Line Number: 662

行号:662

Backtrace:

回溯:

File: C:\xampp\htdocs\Site\application\models\class_model.php Line: 48 Function: where

文件:C:\ xampp \ htdocs \ Site \ application \ models \ class_model.php行:48功能:其中

File: C:\xampp\htdocs\Site\application\controllers\class_con.php Line: 107 Function: update

文件:C:\ xampp \ htdocs \ Site \ application \ controllers \ class_con.php行:107功能:更新

File: C:\xampp\htdocs\Site\index.php Line: 292 Function: require_once

文件:C:\ xampp \ htdocs \ Site \ index.php行:292功能:require_once

here my controller

我的控制器

function edit($id)
{
    $rules = [
        [
            'field' => 'classname',
            'label' => 'Class Name',
            'rules' => 'trim|required'
        ],
        [
            'field' => 'inchargename',
            'label' => 'Incharge Name',
            'rules' => 'trim|required'
        ],
        [
            'field' => 'classstrength',
            'label' => 'Class Strength',
            'rules' => 'trim|required'
        ]
    ];
    $this->form_validation->set_rules($rules);
    $class = $this->class_model->find($id)->row();


    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('admin/class/classEdit',array('class'=>$class));
    }
    else
    {

        $data['classname']      = set_value('classname');
        $data['inchargename']   = set_value('inchargename');
        $data['classstregth']   = set_value('classstregth');

        $this->class_model->update($id,$data);
        $this->session->set_flashdata('message','Class has been Updated Successfully');
        redirect('class_con/index');

    }
}

and here's my model

这是我的模特

public function find($id) {

    $this->db->where('id',$id);
    $row = $this->db->get('class');
    return $row;
}

function update($data, $id)
{
    try{
        $this->db->where('id',$id);
        $this->db->update('class', $data);
        return true;
    }
    catch(Execption $e){
        echo $e->getMessage();
    }

}

and here is my view

这是我的观点

<?php echo form_open_multipart('class_con/edit/'.$class->id); ?>
                        <div class="form-group" id="register-login-group">
                            <label for="classname">Class Name</label>
                            <div class="input-group">
                                <input type="text" class="form-control" id="classname" name="classname" value="<?php echo $class->classname; ?>" placeholder="Class Name">
                                <div class="input-group-addon"><i class="fa fa-pencil"></i></div>
                            </div>
                        </div>
                        <div class="form-group" id="register-login-group">
                            <label for="classname">Incharge Name</label>
                            <div class="input-group">
                                <input type="text" class="form-control" id="inchargename" name="inchargename" value="<?php echo $class->inchargename; ?>" placeholder="Incharge Name">
                                <div class="input-group-addon"><i class="fa fa-pencil"></i></div>
                            </div>
                        </div>
                        <div class="form-group" id="register-login-group">
                            <label for="classname">Class Strength</label>
                            <div class="input-group">
                                <input type="text" class="form-control" id="classstrength" name="classstrength" value="<?php echo $class->classstrength; ?>" placeholder="Class Stregth">
                                <div class="input-group-addon"><i class="fa fa-pencil"></i></div>
                            </div>
                        </div>

                                <button type="submit" class="btn btn-primary">Save</button>
                                <?=anchor('class_con/index','Cancel',['class'=>'btn btn-warning'])?>
                    <?php echo form_close(); ?>

3 个解决方案

#1


1  

In your controller You call the update method in model by this line :

在您的控制器中您可以通过以下行调用模型中的更新方法:

$this->class_model->update($id,$data);

but in your Model you have defined function as :

但在您的模型中,您将函数定义为:

function update($data, $id)
{
    try{
        $this->db->where('id',$id);
        $this->db->update('class', $data);
        return true;
    }
    catch(Execption $e){
        echo $e->getMessage();
    }

}

the problem is with the order of parameter's that you have passed :

问题在于你传递的参数的顺序:

change it to :

将其更改为:

$this->class_model->update($data,$id);

#2


0  

Just remove ->row() from

只需删除 - > row()

 $class = $this->class_model->find($id)->row();

Instead use

而是使用

$class = $this->class_model->find($id);
foreach ($class->result() as $row)
{
    echo $row->colunn_name;
}

And set_value()

和set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function.

允许您设置输入表单或textarea的值。您必须通过函数的第一个参数提供字段名称。

So instead

所以与其

    $data['classname']      = set_value('classname');
    $data['inchargename']   = set_value('inchargename');
    $data['classstregth']   = set_value('classstregth');

Use to store value in $data array

用于在$ data数组中存储值

  $data['classname']      = 'classname';
  $data['inchargename']   = 'inchargename';
  $data['classstregth']   = 'classstregth';

#3


0  

It seems there is a bug in your query.

您的查询中似乎存在错误。

Please trace error using following steps.

请使用以下步骤跟踪错误。

1) use $this->db->last_query(); which prints your query....

1)使用$ this-> db-> last_query();打印您的查询....

2) Run that query in sql, it gives the appropriate row or not.

2)在sql中运行该查询,它是否给出了相应的行。

3) if query works properly, now print_r the o/p variable and trace weather it gives the result or not.

3)如果查询工作正常,现在print_r o / p变量和跟踪天气它给出结果与否。

#1


1  

In your controller You call the update method in model by this line :

在您的控制器中您可以通过以下行调用模型中的更新方法:

$this->class_model->update($id,$data);

but in your Model you have defined function as :

但在您的模型中,您将函数定义为:

function update($data, $id)
{
    try{
        $this->db->where('id',$id);
        $this->db->update('class', $data);
        return true;
    }
    catch(Execption $e){
        echo $e->getMessage();
    }

}

the problem is with the order of parameter's that you have passed :

问题在于你传递的参数的顺序:

change it to :

将其更改为:

$this->class_model->update($data,$id);

#2


0  

Just remove ->row() from

只需删除 - > row()

 $class = $this->class_model->find($id)->row();

Instead use

而是使用

$class = $this->class_model->find($id);
foreach ($class->result() as $row)
{
    echo $row->colunn_name;
}

And set_value()

和set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function.

允许您设置输入表单或textarea的值。您必须通过函数的第一个参数提供字段名称。

So instead

所以与其

    $data['classname']      = set_value('classname');
    $data['inchargename']   = set_value('inchargename');
    $data['classstregth']   = set_value('classstregth');

Use to store value in $data array

用于在$ data数组中存储值

  $data['classname']      = 'classname';
  $data['inchargename']   = 'inchargename';
  $data['classstregth']   = 'classstregth';

#3


0  

It seems there is a bug in your query.

您的查询中似乎存在错误。

Please trace error using following steps.

请使用以下步骤跟踪错误。

1) use $this->db->last_query(); which prints your query....

1)使用$ this-> db-> last_query();打印您的查询....

2) Run that query in sql, it gives the appropriate row or not.

2)在sql中运行该查询,它是否给出了相应的行。

3) if query works properly, now print_r the o/p variable and trace weather it gives the result or not.

3)如果查询工作正常,现在print_r o / p变量和跟踪天气它给出结果与否。