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();
}
Related
I'm new in codeigniter and php,
and am trying to create crud (update)
how to solve this?
thanks in advance
Model :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Role_model extends CI_Model
{
public function DeleteRole($id)
{
$this->db->where('id', $id);
$this->db->delete('user_role');
}
public function GetId($id)
{
return $this->db->get_where('user_role', ['id' => $id])->row_array();
}
public function EditRole()
{
$data = [
"role" => $this->input->post('role' , true)
];
$this->db->where('id', $this->input->post('id'));
$this->db->update('user_role', $data);
}
}
controller :
public function __construct()
{
parent::__construct();
is_logged_in();
$this->load->model('Role_model');
}
public function edit($id)
{
$data['title'] = 'Role';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['user_role'] = $this->Role_model->GetId($id);
$this->form_validation->set_rules('role', 'Role', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('admin/edit', $data);
$this->load->view('templates/footer');
} else {
$this->Role_model->EditRole();
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role Edited!</div>');
redirect('admin/role');
}
}
view :
<div class="card-body">
<?= $this->session->flashdata('message'); ?>
<form action="<?= base_url('admin/edit/');?>" method="post">
<input type="hidden" name="id" value="<?= $user_role['id']; ?>">
<div class="form-group text-gray-900">
<label for="role">Edit Role</label>
<input type="text" class="form-control" id="role" name="Role" value="<?= $user_role['role']; ?>">
<?= form_error('role', ' <small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
and it display like this
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Admin::edit(), 0 passed in
C:\xampp\htdocs\KingflowWP2\system\core\CodeIgniter.php on line 532 and exactly 1 expected
Let me explain to you.
Edit function needs one parameter (i.e. id). In your form, you are submitting the form without id.
You just need to add the id at the end of the URL like below. Suppose id is 2 then you have to add 2.
base_url('admin/edit/2')
<form action="<?= base_url('admin/edit/2');?>" method="post">
You are only passing $id in
$this->usermodel->Role_model($get['id']);
You have to change your function if you want to post value in hidden input. Oner more thing you have to use default argument if you are not sure about the data is present or not. Always check step by step that data is present or not than do anything.
Write echo here so you get the value in hidden field
<input type="hidden" name="id" value="<?php echo $user_role['id']; ?>">
After that change in the function:
public function edit()
{
$id = $this->input->post('id');
$data['title'] = 'Role';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
if(!empty($id)):
$data['user_role'] = $this->Role_model->GetId($id);
else:
//some error message
endif;
$this->form_validation->set_rules('role', 'Role', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('admin/edit', $data);
$this->load->view('templates/footer');
} else {
$this->Role_model->EditRole();
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role Edited!</div>');
redirect('admin/role');
}
}
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()
controller: test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url', 'captcha', 'email'));
$this->load->model('Fetch_data');
}
public function student()
{
$this->load->view('student-dashboard/index');
}
}
view: login.php
<?php
if($this->input->post('login'))
{
$email2 = $this->input->post('email2');
$password2 = $this->input->post('password2');
$this->db->select('email,password');
$this->db->from('students');
$where = "email='$email2' and password = '$password2'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$num = $query->num_rows();
if($num >'0')
{
$this->db->select('email,password,student_id');
$this->db->from('students');
$where = "email='$email2' and password = '$password2'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$data['student_id'] = $this->session->set_userdata('student_id',$result);
if($result == true)
{
redirect('/test/student', $data);
}
}
else
{
echo "<p style='color: red;font-weight: bold;'>Wrong email id or password! </p>";
}
}
?>
<form method="post">
<div class="form-group">
<input type="text" name="email2" id="email2" placeholder="Enter Your Email" class="text-line"/>
</div>
<div class="form-group">
<input type="password" name="password2" id="password2" placeholder="Enter Your Password" class="text-line"/>
</div>
<div class="form-group">
<input type="submit" name="login" id="login" value="Login" class="btn btn-warning"/>
</div>
</form>
I am new in codeigniter. In this code I am creating login form and it work perfectly. Now, I want to store student_id into session variable through which I can give welcome message on my all pages. So, How can I do this ? please help me.
Thank You
Change this line
$data['student_id'] = $this->session->set_userdata('student_id',$result['student_id']);
to this one:
$this->session->set_userdata('student_id',$result['student_id']);
$data['student_id'] = $this->session->userdata('student_id');
You have to load session also
$this->load->library('session');
If you want to add/create session data :
$data = array(
'student_id' => 'id',
'name' => 'name',
<!-- Your parameters -->
);
$this->session->set_userdata($data);
In order to use Session library you should load it in the constructor method in each controller you need it as :
$this->load->library('session');
By doing this, session variables will be available for all views you are loading in this controller.
In your view you can give welcome message with :
<?php echo "Hello ".$this->session->userdata('student_id');?>
Anyway, i suggest you to move your php code from the view (login.php) to the controller (test.php). It eases your work
you can stored the data as array like this change field as you want
$session = array(
'isLoggedIn' => TRUE,
'username' => $result['username'],
'roleid' => $result['roleid'],
'id' => $result['id']
);
$this->session->set_userdata($session);
and retrive data like
$roleid=$this->session->userdata('roleid');
Hi i am a blog page where i will be inserting blogs from admin panel.while inserting blogs into database i need to insert admin username as well into the database for the blogs.How to get the admin username and insert into blogs table.
Controller:
function addblogs()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('blog_title','Blog Title');
$this->form_validation->set_rules('description','Blog Description');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='blogs';
$data['mode']='add';
$this->load->view('templates/template',$data);
}
else
{
$this -> blogs_model -> insertblogs();
$this->flash->success('<h2>blogs Added Successfully!</h2>');
redirect('blogs');
}
}
Model:
function insertblogs()
{
$username = $_SESSION['name'];
$title=$this->input->post('blog_title');
$result = str_replace(" ", "-", $title);
$data=array(
'blog_title'=>$this->input->post('blog_title'),
'blogtitle'=>$result,
'description'=>$this->input->post('description'),
'user'=>$username
);
$this->db->insert('blogs',$data);
}
}
View:
<?php
$form_attributes = array('name'=>'adds', 'id'=>'adds', 'enctype' => "multipart/form-data");
echo form_open('blogs/addblogs',$form_attributes);
?>
<div class="element">
<label for="blogtitle"><font color ="black">Blog Title</font></label>
<input class="text err" type="text" name="blog_title" id="blog_title" value="<?php echo set_value('blog_title');?>"/>
</div>
<div class="element">
<label for="description"><font color ="black">Blog Description</font></label>
<textarea name="description" id="myArea1" rows="4" cols="173"></textarea>
</div> <br/>
<div align="center">
<input type="submit" id="submit" value="Submit" />
</div>
<div class="clear"></div>
<?php echo form_close();?>
Login Controller:
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records']=$this->career_model->get_jobs_list();
$data['mode'] = "all";
$data['mainpage'] = "career";
$this->load->view('templates/template', $data);
}
else{
$this->load->view('login');
}
Login Model:
<?php
class login_model extends MY_Model
{
function login_user($user_name = '', $password=''){
$userdetails = array(
'user_name' => $user_name,
'password' => md5($password),
);
$this->db->where($userdetails);
$query = $this->db->get('login_details');
if($query->num_rows()):
$user = $query->result();
$sess_arry = array(
'user_id' => $user[0]->user_id,
'name' => $user[0]->name
);
$this->session->set_userdata('admin_logged_in', $sess_arry); //add admin details to session
return true;
else:
return false;
endif;
}
}
You can some change your model in insertblogs() method like that :
$this->load->library('session');
$logged_data = $this->session->userdata('admin_logged_in');
$user_id = $logged_data['user_id'];
$user_name = $logged_data['name'];
$username = $user_name;//$_SESSION['name'];
You can get the name of the logged-in user by:
$username = $this->session->userdata('username');
now you can use this $username to insert it in database or you can perform some other functionality.
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