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.
Related
I am getting the following error during fetching of database value:
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: controllers/User.php
Line Number: 184
Backtrace:
File: >Localdrive:\xampp\htdocs\MyRootFolder\application\controllers\User.php
Line: 184
Function: _error_handler
File: Localdrive:\xampp\htdocs\MyRootFolder\application\controllers\User.php
Line: 151
Function: run
File: Localdrive:\xampp\htdocs\MyRootFolder\index.php
Line: 315
Function: require_once
My Controller:
public function changePassword()
{
$this->logged_in();
$data['title'] = "Change Password";
$this->form_validation->set_rules('oldpass', 'Current Password', 'required|callback_password_check');
$this->form_validation->set_rules('newpass', 'New Password', 'required');
$this->form_validation->set_rules('passconf', 'Confirm Password', 'required|matches[newpass]');
$this->form_validation->set_error_delimiters('<div class="error">','</div>');
if($this->form_validation->run() == false){
$user= $this->session->userdata('user');
$data['user'] = $user;
$this->load->view('header', $data);
$this->load->view('change_password', $data);
$this->load->view('footer', $data);
}
else{
$email = $this->session->userdata('email');
$newpass = $this->input->post('newpass');
//$this->User_model->update_user($id, $sessArray)
$this->User_model->update_user($id, array('password' => password_hash($newpass), PASSWORD_BCRYPT));
redirect('User/logout');
}
}
public function password_check($oldpass)
{
//print($oldpass);
//exit();
$email = $this->session->userdata('email');
$user = $this->User_model->get_user($email);
print($user);
exit();
//print($oldpass);
//exit();
if(password_verify($oldpass, $user['password']) == true){
$this->form_validation->set_message('password_check', 'The {field} does not match');
return false;
}
return true;
}
My Model:
function getData(){
$query = $this->db->where('register');
return $query;
}
public function get_user($email)
{
$this->db->where('email', $email);
$query = $this->db->get('register');
return $query->row();
}
public function update_user($email, $sessArray)
{
//$this->db->set($data);
$this->db->where('email', $email);
$this->db->update('register', $sessArray);
}
My View:
<div class="row justify-content-center" style="margin-top: 50px;">
<div class="col-4">
<h1><?php echo $title ?></h1>
<?php echo form_open('User/changePassword', array('id' => 'passwordForm'))?>
<div class="form-group">
<input type="password" name="oldpass" id="oldpass" class="form-control" placeholder="Current Password" />
<?php echo form_error('oldpass', '<div class="error">', '</div>')?>
</div>
<div class="form-group">
<input type="password" name="newpass" id="newpass" class="form-control" placeholder="New Password" />
<?php echo form_error('newpass', '<div class="error">', '</div>')?>
</div>
<div class="form-group">
<input type="password" name="passconf" id="passconf" class="form-control" placeholder="Confirm Password" />
<?php echo form_error('passconf', '<div class="error">', '</div>')?>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Change Password</button>
</div>
<?php echo form_close(); ?>
</div>
You are requesting the data as an object in your model but trying to us it as an array. Either change this line: if(password_verify($oldpass, $user->password) == true){ to use the fetch as object or change get_user func to return $query->row_array(); to use it as an array. P.s.: for debug use var_dump both for array and object instead of print
I am using Laravel-5.8 for a Web Application. I am trying to apply Rules and Requests in Laravel Validation. I have these codes:
rules
public function rules()
{
return [
'organization_code' => [
'required',
'string',
'min:2',
'max:20',
Rule::unique('org_companies', 'organization_code')->ignore($this->route('company')->id)
],
'organization_name' => [
'required',
'string',
'min:3',
'max:100',
Rule::unique('org_companies', 'organization_name')->ignore($this->route('company')->id)
],
'website' => [
'nullable',
'url',
'max:100',
Rule::unique('org_companies', 'website')->ignore($this->route('company')->id)
],
'org_image' => 'nullable|image|mimes:jpeg,bmp,png,gif|max:2048',
'total_employees' => 'nullable|numeric|digits_between:0,10',
'registration_number' => [
'nullable',
'string',
'max:50',
Rule::unique('org_companies', 'registration_number')->ignore($this->route('company')->id)
],
'phone_number' => [
'nullable',
'numeric',
'phone:NG',
Rule::unique('org_companies', 'phone_number')->ignore($this->route('company')->id)
],
'secondary_phone' => [
'nullable',
'numeric',
'phone:NG',
Rule::unique('org_companies', 'secondary_phone')->ignore($this->route('company')->id)
],
'email' => [
'nullable',
'email',
'max:80',
Rule::unique('org_companies', 'email')->ignore($this->route('company')->id)
],
'secondary_email' => [
'nullable',
'email',
'max:80',
Rule::unique('org_companies', 'secondary_email')->ignore($this->route('company')->id)
],
];
}
Controller
public function edit($id)
{
$company = OrgCompany::where('id', $id)->first();
$countries = ConfigCountries::all();
return view('organization.companies.edit')->with('company', $company)->with('countries', $countries);
}
public function update(UpdateCompanyRequest $request, $id)
{
try {
$orgStartDate = Carbon::parse($request->org_start_date);
$company = OrgCompany::find($id);
$company->organization_code = $request->organization_code;
$company->organization_name = $request->organization_name;
$company->website = $request->website;
$company->org_description = $request->org_description;
$company->total_employees = $request->total_employees;
$company->registration_number = $request->registration_number;
$company->org_start_date = $orgStartDate;
$company->phone_number = $request->phone_number;
$company->secondary_phone = $request->secondary_phone;
$company->email = $request->email;
$company->secondary_email = $request->secondary_email;
$company->country_id = $request->country_id;
if ($request->org_image != "") {
$org_image = $request->file('org_image');
$new_name = rand() . '.' . $org_image->getClientOriginalExtension();
$org_image->move(public_path('storage/companies/image'), $new_name);
$company->org_image = $new_name;
}
$company->save();
Session::flash('success', 'Company is updated successfully');
return redirect()->route('organization.companies.index');
} catch (Exception $exception) {
Session::flash('danger', 'Action failed!');
return redirect()->route('organization.companies.index');
}
}
view blade
<div class="card-body">
<form action="{{route('organization.companies.update', ['id'=>$company->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<span style="color:blue;"><h4 class="box-title"><b>Company Information</b></h4></span>
<hr class="m-t-0 m-b-40">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3"> Company Code<span style="color:red;">*</span></label>
<div class="col-md-9 controls">
<input type="text" name="organization_code" placeholder="Enter company code here" class="form-control" value="{{old('organization_code',$company->organization_code)}}">
</div>
</div>
</div>
<!--/span-->
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3"> Company Name<span style="color:red;">*</span></label>
<div class="col-md-9 controls">
<input type="text" name="organization_name" placeholder="Enter company name here" class="form-control" value="{{old('organization_name',$company->organization_name)}}">
</div>
</div>
</div>
<!--/span-->
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('organization.companies.index')}}'" class="btn btn-default">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
When I clicked on the save button to update, I got this error:
Trying to get property 'id' of non-object
Then, in the rules this code is highlighted:
Rule::unique('org_companies', 'organization_code')->ignore($this->route('company')->id)
How do I get it resolved?
Thank you.
Change
public function update(UpdateCompanyRequest $request, $id)
to
public function update(UpdateCompanyRequest $request, OrgCompany $company)
With your current route $id is just an ID and company doesn't exist.
Your rules are looking for it to be an object.
If you put OrgCompany $company Laravel will automatically find it for you.
This also allows you to simplify your entire update method.
public function update(UpdateCompanyRequest $request, OrgCompany $company)
{
try {
$orgStartDate = Carbon::parse($request->org_start_date);
// this will already be assigned and is unnecessary ->
// $company = OrgCompany::find($id);
// your request object already contains a validated, clean array.
$validated = $request->validated();
$company->fill($validated);
$company->org_start_date = $orgStartDate;
if ($request->org_image != "") {
$org_image = $request->file('org_image');
$new_name = rand() . '.' . $org_image->getClientOriginalExtension();
$org_image->move(public_path('storage/companies/image'), $new_name);
$company->org_image = $new_name;
}
$company->save();
Session::flash('success', 'Company is updated successfully');
return redirect()->route('organization.companies.index');
} catch (Exception $exception) {
Session::flash('danger', 'Action failed!');
return redirect()->route('organization.companies.index');
}
}
You can also update the signature of your edit method to automatically inject the OrgCompany in the same way.
public function edit(OrgCompany $company)
{
// already exists now -> $company = OrgCompany::where('id', $id)->first();
$countries = ConfigCountries::all();
return view('organization.companies.edit')->with('company', $company)->with('countries', $countries);
}
I am working on a basic (just 2 tables: authors and posts) blog application in Codeigniter 3.1.8.
I have an edit post functionality, using an update form:
<?php echo form_open("posts/update"); ?>
<input type="hidden" name="id" id="pid" value="<?php echo $post->id; ?>">
<div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
<input type="text" name="title" id="title" class="form-control" placeholder="Title" value="<?php echo $post->title; ?>">
<?php if(form_error('title')) echo form_error('title'); ?>
</div>
<div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
<input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription" value="<?php echo $post->description; ?>">
<?php if(form_error('desc')) echo form_error('desc'); ?>
</div>
<div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
<textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"><?php echo $post->content; ?></textarea>
<?php if(form_error('body')) echo form_error('body'); ?>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
In the Posts_model model I have the method responsible with updating the post:
public function update_post() {
$data = [
'title' => $this->input->post('title'),
'description' => $this->input->post('desc'),
'content' => $this->input->post('body')
];
$this->db->where('id', $this->input->post('id'));
return $this->db->update('posts', $data);
}
In the Posts controller, I have 2 methods, for editing and updating the post:
public function edit($id) {
$data = $this->Static_model->get_static_data();
$data['post'] = $this->Posts_model->get_post($id);
$data['tagline'] = 'Edit the post "' . $data['post']->title . '"';
$this->load->view('partials/header', $data);
$this->load->view('edit');
$this->load->view('partials/footer');
}
public function update() {
$this->Posts_model->update_post();
// Redirect to the updated post
}
In my update() method, I was unable to redirect to the updated post. The redirect($this->agent->referrer()); line only brings me back to the update form. Where I want to redirect to is the just updated post. How do I do that?
Post vars should be handled in your controller:
public function update() {
$id = $this->input->post('id');
$data = [
'title' => $this->input->post('title'),
'description' => $this->input->post('desc'),
'content' => $this->input->post('body')
];
// Update post
$this->Posts_model->update_post($id, $data);
// Redirect to the updated post
redirect('posts/post/' . $id);
}
Model:
public function update_post($id, $data) {
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
Note: if you want to do it your way, then simply return the id in the model function and redirect based on the return of the function in update()
I am new to CodeIgniter. I am using a form with Form Validation. My problem is when I press "Edit details", an ERROR appears. "Trying to get property of non-object".
This is my view: user/edit_user.php
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<h1>EDIT YOUR DETAILS</h1>
<?php if (validation_errors()){ ?>
<div class="alert alert-danger">
<?= validation_errors() ?>
</div>
<?php } ?>
<form method="POST" action="<?=base_url().'user/do_edit_user'?>">
<input value="<?= $user->id ?>" name="id" type="hidden">
<input value="<?= $user->name ?>" type="text" name="name" class="form-control" placeholder="Name"> <br>
<input value="<?= $user->password ?>" type="password" name="password" class="form-control" placeholder="Password"> <br>
<input value="<?= $user->email ?>" type="text" name="email" class="form-control" placeholder="Email"> <br>
<input value="<?= $user->number ?>" type="number" name="number" class="form-control" placeholder="Number"> <br>
<input value="<?= $user->university ?>" type="text" name="university" class="form-control" placeholder="University"> <br>
<button class="btn btn-success" type="submit">Submit</button>
</form>
</div>
<div class="col-md-3">
</div>
This is my controller: User.php
public function edit_details(){
$id = $this->uri->segment(3);
$data['user'] = $this->AdminModel->getUser($id);
$this->load->view('includes/header');
$this->load->view('includes/navbar');
$this->load->view('users/edit_user', $data);
$this->load->view('includes/footer');
}
public function display(){
$cond = array (
'email' => $this->input->post('email'),
'password' => sha1($this->input->post('password'))
);
$data['user'] = $this->UserModel->getUser($cond);
if(!($data))
{
redirect('user/index');
}
else
{
$this->load->view('includes/header');
$this->load->view('includes/navbar');
$this->load->view('users/display', $data);
$this->load->view('includes/footer');
}
}
And my last controller for do_edit_user`
public function do_edit_user(){
$id = $this->input->post('id');
$user = array(
'name' => $this->input->post('name'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'),
'number' => $this->input->post('number'),
'university' => $this->input->post('university')
);
$this->form_validation->set_rules('name', 'Name', 'required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('number', 'Number', 'max_length[11]');
$this->form_validation->set_rules('university', 'University', 'min_length[8]');
if ($this->form_validation->run() == FALSE)
{
$this->edit_details();
}
else
{
$this->AdminModel->updateUser($id, $user);
redirect('user/display');
}
}
public function activate(){
$cond = array(
'activation_code' => $this->uri->segment(3)
);
if(!($this->UserModel->checkAccount($cond)==null)){
echo 'successfully registered email';
}else{
echo 'invalid';
}
}
What am I missing or need to change? Sorry for my format if this is wrong. Correct me if there was wrong in my post and code.
Actual Error is A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: users/edit_user.php
Line Number: 16
Line Number: 17
Line Number: 18
Line Number: 19
Line Number: 20
Line Number: 21
My UserModel
public function insertUser($user){
$this->db->insert('tbluser', $user);
}
public function checkAccount($cond){
$this->db->where($cond);
$this->db->update('tbluser', array('status'=>'active'));
$q = $this->db->get_where('tbluser', $cond);
return $q->row();
}
public function getUser($cond){
$this->db->where($cond);
$q = $this->db->get('tbluser');
return $q->row();
}
public function getUser($id){
$q = $this->db->get_where('tbluser', array('id' => $id));
return $q->row();
}
You should handle the null of $user object.
Following things are cheats and will not give you errors.
<?=#$user->id using a # sign if you are not sure about $user object
OR
Assign the user to a object variable.
$data['user'] = new stdClass();
$data['user'] = $this->UserModel->getUser($cond);
Maybe it's because you return a string or an array from UserModel->getUser() method, like return $query->result_array() or return $string, to $data['user'] in User controller instead of return $query->result(). Check it.
Update 1
I'm newbie too, but you can try this if you still need help:
User.php (change to)
$email => $this->input->post('email'); //this
$password => sha1($this->input->post('password')); //this
$data['user'] = $this->UserModel->getUser($email, $password); //or this
and your UserModel.php (change to)
public function getUser($email, $password){ // and this are
$this->db->where('email' => $email); // not a
$this->db->where('password' => $password); // nesessary changes but you can try it
$q = $this->db->get('tbluser');
return $q->result(); // try result() instead of row()
}
$user['id']
$user['name']
$user['password']
$user['email']
$user['number']
$user['university']
try to do like this when you return row_array() or result()
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
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;
}
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
$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