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()
Related
I want to Login whenever I click the submit button, I get all information on username and password to log in and can get in into the dashboard. can somebody help me?
but I got an error like this
Undefined property: App\Controllers\Admin::$user
this is my code:
public function __construct()
{
$this->user = new UserModel();
$this->sponsor = new SponsorModel();
$this->auditorium = new auditoriumModel();
}
public function index()
{
$user = $this->user->findAll();
$audVid = $this->auditorium->findAll();
$sponsorData = $this->sponsor->findAll();
$data = [
'user' => $user,
'sponsorData' => $sponsorData,
'audvid' => $audVid
];
// dd($sponsorData);
echo view('templates/header');
echo view('login', $data);
echo view('templates/footer');
}
public function doLogin()
{
$email
= $this->request->getVar('email');
$password =
$this->request->getVar('password');
$userData = $this->user->where('email', $email)
->where('password', $password)
->findAll();
if ($userData == null) {
return redirect()->to('/admin');
} else {
$_SESSION['logonUser'] = 'aktif';
return redirect()->to('/adminDashboard', $userData);
}
}
}
this is my login view
<form id="login-form" class="form" action="/admin/doLogin" method="post">
<h3 class="text-center text-info">Login Admin</h3>
<div class="form-group">
<label for="username" class="text-info">Username:</label><br>
<input type="text" name="username" id="username" class="form-control">
</div>
<div class="form-group">
<label for="password" class="text-info">Password:</label><br>
<input type="text" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<labezl for="remember-me" class="text-info"><span>Remember me</span> <span><input id="remember-me" name="remember-me" type="checkbox"></span></labezl><br>
<input type="submit" class="btn btn-info btn-md" value="submit" href="/adminDashboard">
</div>
<div id="register-link" class="text-right">
Register here
</div>
</form>
can somebody help me to solve my code?
thanks
$userData = $this->user->where('email', $email)
->where('password', $password)
->findAll();
findAll() will get you an array of rows that applies to your criteria. In your case will try to find all users where email and password are correct, but it will still return you an array of them, not just one. You need to be using find() instead.
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'm trying create an edit profile function for a user. When the user submits the form, it updates the data in the database. But when I try to access the edit profile page I get this error:
Type: ArgumentCountError
Message: Too few arguments to function Profile::editProfile(), 0 passed in /Applications/MAMP/htdocs/Capstone/system/core/CodeIgniter.php on line 532 and exactly 1 expected
I'm passing $id in the function, so I'm not sure why this is happening.
Model:
public function updateUser($id)
{
$data = array(
'age' => $this->input->post('age'),
'location' => $this->input->post('location'),
'favouritebeer' => $this->input->post('favouritebeer'),
'website' => $this->input->post('website'),
'bio' => $this->input->post('bio')
);
$this->db->where('id', $id);
$this->db->update('users', $data);
return $id;
}
public function getUser($id)
{
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('users');
$query = $this->db->get();
return $query-row();
}
Controller:
public function editProfile($id)
{
$this->load->model('user_model');
$this->load->view('templates/header');
$this->load->view('pages/editprofile');
$this->load->view('templates/footer');
if(isset($_POST['updatechanges'])) {
if($this->user_model->getUser($id)) {
$this->session->set_flashdata('success', 'Profile updated!');
redirect('profile', 'refresh');
}
}
}
View:
<div class ="container">
<?php
$con=mysqli_connect("localhost","root","admin","Mydb");
$sql = mysqli_query($con, "SELECT id, username, age, location, favouritebeer, website, bio FROM users WHERE username = '" . $_SESSION['username'] . "'");
$row = mysqli_fetch_array($sql);
?>
<h2><?php echo $_SESSION['username'];?>'s Profile</h2>
Logout
</br>
Change password
</br>
Change security question
</br>
</br>
<body>
<form method ="POST">
<h4>Age:</h4>
<input type="age" class="form-control" name="age" id="ageinput" value="<?php echo $row['age']; ?>">
<h4>Location:</h4>
<input type="location" class="form-control" name="location" id="locationinput" value="<?php echo $row['location']; ?>">
<h4>Favourite beer:</h4>
<input type="beer" class="form-control" name="favouritebeer" id="beerinput" value="<?php echo $row['favouritebeer']; ?>">
<h4>Website:</h4>
<input type="website" class="form-control" name="website" id="websiteinput" value="<?php echo $row['website']; ?>">
<h3>Bio: </h3>
<input type="bio" class="form-control" name="bio" id="bioinput" value="<?php echo $row['bio']; ?>">
Save Changes
</form>
</div>
if you are calling this editProfile() function from view try the following code :
in view :
edit
and in controller :
function editProfile() {
$id = $this->uri->segment(3);
// do more
}
and if you are calling this function from another function in the same controller try this :
$this->editProfile(1);
Try this code. The below mentioned function have one errors in return place.
public function getUser($id)
{
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('users');
$query = $this->db->get();
return $query->row();
}
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
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.