简体   繁体   中英

array to string conversion error in codeigniter 3.0.1

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

Line Number: 662

Backtrace:

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

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

File: C:\\xampp\\htdocs\\Site\\index.php Line: 292 Function: 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(); ?>

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);

Just remove ->row() from

 $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()

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.

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['classname']      = 'classname';
  $data['inchargename']   = 'inchargename';
  $data['classstregth']   = 'classstregth';

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....

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM