0

im trying to have an edit/update module in my system. But, it seems i cant figure it out why im getting array to string conversion error. Below are the model,view,controller codes.

Line error in model

function updatebyid($userid, $data)
{
    $this->db->where('id', $userid); **i get the error in this line in my model**

Line error in controller

$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
    if ($this->model_user->updatebyid($userid, $data)) **AND IN THIS LINE TOO IN MY CONTROLLER**

My model

function get_user_by_id($id)
{
    $this->db->where('id', $id);
    $query = $this->db->get('users');
    return $query->result();
}
function updatebyid($userid, $data)
{
    $this->db->where('id', $userid);
    $query = $this->db->update('users', $data);
    return $query->result();
    //return $query->row()->query;
}

My view

</div>
    <div class="row well" style="color: black; margin-top: 15px;">
        <h2>Edit Profile</h2>
            <?php $attributes = array("name" => "registerform");
        echo form_open("account/edituserinfo/", $attributes);?>

        <div class="form-group">

            <div class="col-sm-4">
            <label for="fname">First Name</label>
            <input class="form-control" name="fname" required type="text" value="<?php echo $fname; ?>" />
            <span class="text-danger"><?php echo form_error('fname'); ?></span>
            </div>

            <div class="col-sm-4">
            <label for="mname">Middle Name</label>
            <input class="form-control" name="mname" required placeholder="Middle Name" type="text" value="<?php echo $mname; ?>" />
            <span class="text-danger"><?php echo form_error('mname'); ?></span>
            </div>

            <div class="col-sm-4">
            <label for="lname">Last Name</label>
            <input class="form-control" name="lname" required placeholder="Last Name" type="text" value="<?php echo $lname; ?>" />
            <span class="text-danger"><?php echo form_error('lname'); ?></span>
            </div>
        </div>
<div class="input-group" style="padding-top: 15px; margin-left: 15px">
            <button name="submit" type="submit" class="btn btn-success">Update</button>
            <!--input class="form-control" name="id" placeholder="id" type="hidden" value="<?php echo $id; ?>" /-->
        </div>

        <?php echo form_close(); ?>
        <?php echo $this->session->flashdata('msg'); ?>
    </div>
    </div>

My controller

function index()
{
    $this->load->view('include/headnav');
    $details = $this->model_user->get_user_by_id($this->session->userdata('id'));
    $data['name'] = $details[0]->fname . " " . $details[0]->lname;
    $data['level'] = $this->session->userdata('level');
    $data['fname'] = $details[0]->fname;
    $data['mname'] = $details[0]->mname;
    $data['lname'] = $details[0]->lname;
    $data['gender'] = $details[0]->gender;
    $data['email'] = $details[0]->email;
    $data['mobileNum'] = $details[0]->mobileNum;
    $data['landlineNum'] = $details[0]->landlineNum;
    $data['homeaddress'] = $details[0]->homeaddress;
    //$data['position'] = $details[0]->position;
    //$data['institution'] = $details[0]->institution;
    //$data['institutionAddr'] = $details[0]->institutionAddr;
    //$data['institutionNum'] = $details[0]->institutionNum;
    $this->load->view('view_account', $data);
    $this->load->view('include/footernav');
}
function edituserinfo()
{
    $this->load->view('include/headnav');

    $data = array(
            'lname' => $this->input->post('lname'),
            'fname' => $this->input->post('fname'),
            'mname' => $this->input->post('mname'),
            'gender' => $this->input->post('gender'),
            'homeaddress' => $this->input->post('homeaddress'),
            'mobileNum' => $this->input->post('mobileNum'),
            //'password' => $this->input->post('password'),
            //'cPassword' => $this->input->post('cPassword'),
            //'institution' => $this->input->post('institution'),
            //'institutionAddr' => $this->input->post('institutionAddr'),
            //'institutionNum' => $this->input->post('institutionNum'),
            //'position' => $this->input->post('position'),
        );

    $userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
    if ($this->model_user->updatebyid($userid, $data))
        {

            $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Profile edited.</div>');
            redirect('account/edituserinfo');
        }
        else
        {
            // error
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Error. Check credentials.</div>');
            redirect('account/edituserinfo');
        }
}

    function updateuinfoview()
{
    $this->load->view('include/headnav');
    $details = $this->model_user->get_user_by_id($this->session->userdata('id'));
    //$data['fname'] = $details[0]->fname . " " . $details[0]->lname;
    $data['fname'] = $details[0]->fname;
    $data['lname'] = $details[0]->lname;
    $data['mname'] = $details[0]->mname;
    $data['gender'] = $details[0]->gender;
    $data['email'] = $details[0]->email;
    $data['mobileNum'] = $details[0]->mobileNum;
    $data['landlineNum'] = $details[0]->landlineNum;
    $data['homeaddress'] = $details[0]->homeaddress;
    //$data['position'] = $details[0]->position;
    //$data['institution'] = $details[0]->institution;
    //$data['institutionAddr'] = $details[0]->institutionAddr;
    //$data['institutionNum'] = $details[0]->institutionNum;
    $this->load->view('view_editprofile', $data);
    $this->load->view('include/footernav');
}

This is the error message

A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: database/DB_query_builder.php Line Number: 669

Backtrace: File: C:\xampp\htdocs\THTF.6\application\models\model_user.php Line: 73 Function: where File: C:\xampp\htdocs\THTF.6\application\controllers\Account.php Line: 74 Function: updatebyid File: C:\xampp\htdocs\THTF.6\index.php Line: 315 Function: require_once

A Database Error Occurred Error Number: 1054 Unknown column 'Array' in 'where clause' UPDATE users SET lname = 'Taz', fname = 'Vinny', mname = 'Paz', gender = 'Male', homeaddress = 'USA', mobileNum = '4123' WHERE id = Array

Filename: C:/xampp/htdocs/THTF.6/system/database/DB_driver.php Line Number: 691

5
  • You're accessing an array as a string. What's the actual error? Commented Mar 12, 2017 at 16:54
  • 1
    Please show us the complete error message complete with line number and then indicate in your code which is the line causing the problem Commented Mar 12, 2017 at 16:58
  • when i try to update the user's data i get unknown column 'Array' in my where clause Commented Mar 12, 2017 at 16:58
  • my bad, i edited my post just now. Commented Mar 12, 2017 at 17:00
  • I think you will find the Database Error more important to fix!!!!!!!! Commented Mar 12, 2017 at 17:12

3 Answers 3

1

You are getting array to string conversion error because your model :

$this->model_user->get_user_by_id($this->session->userdata('id'))

returns an array of objects. To remedy your problem, you could loop the result or if you only need the first row of your result you could do this:

$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid[0]->id, $data))

or perhaps you can modify your model like this:

function get_user_by_id($id)
{
    $this->db->where('id', $id);
    $query = $this->db->get('users');
    return $query->row(); // use row if you only want the first row of the result of your query
}

controller:

$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid->id, $data))

***************************************************************************************
or a more comprehensive model so you wont need to change your controller:

function get_user_by_id($id)
{
    $this->db->where('id', $id);
    $query = $this->db->get('users');
    $result = $query->row();
    return ($result) ? $result->id : false; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are passing array of user details instead of user id in update

change this line

$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));

to

$userid = $this->session->userdata('id');

in function edituserinfo() of controller

3 Comments

Thanks for the answer. But, it didn't work. i got this error Severity: Error Message: Call to a member function result() on boolean Filename: models/model_user.php Line Number: 75
return $query->result(); in this line in function updatebyid of model
remove return $query->result(); from updatebyid function. Update method will not return result
0
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));

In the above line you are querying the database which will return an array.

Instead set the user ID as below

$userid = $this->session->userdata('id');

Your Answer

3 Comments

Thanks for the answer. But still it didn't work im getting this error Severity: Error Message: Call to a member function result() on boolean Filename: models/model_user.php Line Number: 75
return $query->result(); in this line in function updatebyid of model
$userid = $this->session->userdata('id'); $this->model_user->updatebyid($userid, $data); in your controller just write this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.