Too few arguments to function Admin::edit(), - php

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

Related

Codeigniter: ArgumentErrorCount

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

Trying to get property of non-object CodeIgniter

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

Prevent Duplicate Entry php with Codeigniter

I am trying to prevent duplicate entries with codeigniter, but it doesn't work anymore.
The data still insert to database despite the same name.
This is the controller code
public function add(){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('add_pbk_g', $data);
}
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$this->db->insert('pbk_groups',array('Name'=> $this->input->post('namegroup')));
$this->load->model('mpbk_grup');
if($this->mpbk_grup->check_grup_exist('$Name')){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('layout/header', $title);
$this->load->view('add_pbk_g');
$this->load->view('layout/footer');
} else{
$this->mpbk_grup->add;
redirect('cpbk_grup/index');
}
}
}
this is the Model..
function add($data){
return $this->db->create('pbk_groups', $data);
}
function check_grup_exist($namegroup){
$this->db->where('Name', $namegroup);
$this->db->from('pbk_groups');
$query = $this->db->get();
if($query->num_rows() >0){
return $query->result();
} else {
return $query->result();
//return False;
}
}
and this is the view
<form method="post" class="form-horizontal" action="<?php echo site_url('cpbk_grup/save');?>">
<div class="box-body">
<?php echo validation_errors(); ?>
<div class="form-group">
<label class="col-sm-2 control-label">Nama Group</label>
<div class="col-sm-10">
<input type="text" name="namegroup" required="" class="form-control" placeholder="Nama Group">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" value="submit" class="btn btn-info pull-right">Save</button>
Kembali
</div>
<!-- /.box-footer -->
</form>
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$data = array ('Name' => $this->input->post('namegroup'));
$this->mpbk_grup->check_grup_exist($data);
}
}
Where does $Name on this line come from?
if($this->mpbk_grup->check_grup_exist('$Name')){
Also it should not have quotes around it, should be...
if($this->mpbk_grup->check_grup_exist($Name)){
Try doing this inside save function
Controller Code:
$data = array('Name' => $this->input->post('namegroup'));
$this->Model_Name->check_grup_exist($data);
My Model
Model_Name
$this->main_table = 'pbk_groups';
public function check_grup_exist($data)
{
$query = $this->db->get_where($this->main_table, array('Name' => $data['Name']));
if ($query->num_rows() == 0)
{
$this->db->insert($this->main_table, $data);
return $this->db->insert_id();
}
else
{
return false;
}
Checking duplication data before inserting the data. Let me know if any query occurs.

Codeigniter form validation is not showing errors

I'm new to Codegniter so go easy on me. I'm building a simple login form and I have successfully redirected to a page when the login credentials are correct. However, if I submit an empty form I get no error messages. I'm also using set_value in the form field and codeigniter does not refill what the user inputted once the form is submitted. Somehow that data is being cleared. Here are a few things i've done for clarity sake.
Auto-loaded the form_validation library
Auto-loaded form helper
Echoed validation_errors above form
account.php (controller)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Account extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user');
}
public function index() {
$this->load->view('home');
}
public function validate() {
$this->form_validation->set_rules('username', 'username', 'xss_clean|required');
$this->form_validation->set_rules('password', 'password', 'xss_clean|required|md5');
$user_data = array(
'username' => $this->input->post('username'),
'password' => MD5($this->input->post('password'))
);
if ($this->form_validation->run() == FALSE)
{
$data['title'] = "Login Fool";
$this->load->view('templates/header', $data);
$data['contents'] = $this->load->view('login_view', $data, TRUE);
$this->load->view('page', $data);
$this->load->view('templates/footer');
}
else
{
$validated = $this->user->validate($user_data['username'], $user_data['password']);
if($validated){
redirect(base_url() . 'account');
}
else{
$this->session->set_flashdata('LoginError', 'Sorry, your credentials are incorrect.');
redirect(base_url() . 'account/login');
}
}
}
public function login() {
$data['title'] = "Login Fool";
$this->load->view('templates/header', $data);
$data['contents'] = $this->load->view('login_view', NULL, TRUE);
$this->load->view('page', $data);
$this->load->view('templates/footer');
}
}
login_view.php (view)
<h1>Login Now</h1>
<?php
if(validation_errors() != false) {
echo "<div id='errors'>" . validation_errors() . "</div>" ;
}
?>
<?= ($this->session->flashdata('LoginError')) ? '<p class="LoginError">' . $this->session->flashdata('LoginError') . '</p>' : NULL ?>
<?php echo form_open('account/validate'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" value="<?php echo set_value('username'); ?>"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password" value="<?php echo set_value('password'); ?>"/>
<br/>
<input type="submit" value="Login"/>
</form>
Any ideas why the errors won't show above the form?
Turns out my model was the culprit. It was extending CI_Controller not CI_Model. Thanks to everyone who took a look at this. This code works.

insert and update in single form in CodeIgniter

Here I have controller news.php:
<?php
class News extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index(){
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = "News in Detail(s)";
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item 22';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
$data['updateid'] = '' ;
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
if($update == 1){
redirect('/news', 'location');
}
}
}
public function update($id){
$this->load->helper('form');
$this->load->library('form_validation');
$data['updateid'] = $id;
$data['title'] = "Update News in Detail(s)";
$data['update'] = $this->news_model->edit_load_data($id);
$this->load->view('news/create',$data);
if ($this->input->post('submit')) {
$update = $this->news_model->update_news($id);
if($update == 1){
redirect('/news', 'location');
}
}
}
}
?>
Here my model file news_model.php:
<?php
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_news($slug= false){
if($slug == false){
$query= $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
//'text' => $this->input->post('text')
'text' => $this->input->post('select')
);
return $this->db->insert('news', $data);
}
public function edit_load_data($id){
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
public function update_news($id){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
//'text' => $this->input->post('text')
'text' => $this->input->post('select')
);
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
}
?>
Here my form create.php file:
<?php echo validation_errors();
echo $updateid;
?>
<?php if($updateid == ''){ ?>
<?php echo form_open('news/create'); ?>
<?php } else { ?>
<?php echo form_open('news/update/'.$updateid); ?>
<?php } ?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />
<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />
<label for="text">Select</label>
<select name="select">
<option <?php if($update['text']=='text1'){ echo "selected";} ?> value="text1">text1</option>
<option <?php if( $update['text']=='text2'){ echo "selected";} ?> value="text2">text2</option>
</select>
<br />
<input type="submit" name="submit" value="Create news item" />
</form>
Here when I am going news/create in gives error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: update
Filename: news/create.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\mylab\application\views\news\create.php Line: 15
Function: _error_handler
File: C:\xampp\htdocs\mylab\application\controllers\News.php Line: 54
Function: view
File: C:\xampp\htdocs\mylab\index.php Line: 315 Function: require_once
" />
What can I do for that? I want add and edit for same file create.php
is it possible there?
I don't know that this will produce the outcome you want but it will prevent the use of undefined variables.
<?php
echo validation_errors();
$updateid = isset($updateid) ? $updateid : '';
echo $updateid;
if($updateid == '')
{
echo form_open('news/create');
}
else
{
echo form_open('news/update/'.$updateid);
}
if(!isset($update))
{
$update['title'] = NULL;
$update['slug'] = NULL;
$update['text'] = NULL;
}
?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />
<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />
<label for="text">Select</label>
<select name="select">
<option <?php
if($update['text'] == 'text1')
{
echo "selected";
}
?> value="text1">text1</option>
<option <?php
if($update['text'] == 'text2')
{
echo "selected";
}
?> value="text2">text2</option>
</select>
<br />
<input type="submit" name="submit" value="Create news item" />
</form>
You should probably modify your model so that it returns a default array if/when the inputs are not present. I am not able to identify where you are sending $update to the view either.
I think you're not returning $update['title'], $update['text'], etc.. You are returning $title, $text...
Try changing $update['title'] to $title in create.php to see what
happens

Categories