This is my home.php file in this controller I am facing Fatal error: Uncaught ArgumentCountError:
I have also attached a screenshot, In this screenshot error mentioned, You can easily understand the error through this screenshot, I am newbie in CodeIgniter.
[
I upload only my controller related file.
<?php
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('select');
$this->load->helper(array('form', 'url', 'html'));
$this->load->library(array('form_validation', 'session'));
}
function records() {
$ytl['dta'] = $this->users->datas();
//$this->load->view('info',$ytl);
//echo "<pre>controller";print_r($ytl);
}
function primezone() {
$ytl['dta'] = $this->users->datas();
echo "<pre>controller";
print_r($ytl);
}
function form($id) {
//die($id);
if ($this->input->post()) {
//echo"<pre>";print_r( $this->input->post());die;
$this->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|callback_check_picture[lname]');
//$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|callback_valid_phone_number[mobile]');
$this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>');
if ($this->form_validation->run() == TRUE) {
$data = [
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'message' => $this->input->post('message'),
];
if (empty($id)) {
$ytl = $this->select->insertdata($data);
} else {
$ytl = $this->select->updatedata($data, $id);
}
if ($ytl) {
$this->session->set_flashdata('message', 'Successfully Added.');
redirect('home');
}
} else {
//$this->load->view('form');
}
}
$ytl['dta'] = $this->select->getDataById($id);
//echo "<pre>";print_r($ytl);die;
$this->load->view('form', $ytl);
}
public function check_picture($a) {
if ( ! is_numeric($a)) {
return TRUE;
} else {
$this->form_validation->set_message('check_picture', 'Please enter only char value');
return FALSE;
}
}
function valid_phone_number($value) {
$value = strlen($value);
//echo $value;die;
if ($value == 10) {
return TRUE;
} else {
$this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10}
return FALSE;
}
}
public function index() {
//load the database
//$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h'] = $this->select->select();
//return the data in view
$this->load->view('fetch_view', $data);
}
public function delete() {
$this->load->model('select');
$id = $this->input->get('id');
if ($this->select->deleteuser($id)) {
$data['data'] = $this->select->getuser();
$this->load->view('fetch_view', $data);
}
}
}
The error you have mention is normally came when there is mismatch in param mention in function and the passing ones.
According to your code
public function form($id)
$id is required and you are not passing that.
So change your code to
public function form($id=null)
Set default id value in your controller function like
function form($id = null){
....
}
Related
I want to make form validation on codeigniter
this is the code from alternatif.php on controller
class Alternatif extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model("Alternatif_model");
$this->load->model("User_model");
if (!$this->session->userdata('email')) {
redirect('auth');
}
$data['user'] = $this->db->get_where('users', ['email' => $this->session->userdata('email')])->row_array();
}
public function tambahAlternatif()
{
$data['title'] = 'Tambah Alternatif';
$data['user'] = $this->db->get_where('users', ['email' => $this->session->userdata('email')])->row_array();
if ($this->input->post('nambahAlternatif')) {
foreach ($this->input->post() as $key => $value) {
if (strpos($key, "id_aspek_teknik-") !== false) {
$k = str_replace('id_aspek_teknik-', '', $key);
$this->Alternatif_model->tambahNilai($k);
}
}
$this->Alternatif_model->tambahAlternatif();
$this->session->set_flashdata('flash', 'ditambahkan');
redirect('alternatif');
} else {
code
}
}
I want to ask where I can add the form validation on codeigniter?
Add validation on Controller like:
public function tambahAlternatif()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->form_validation->run() == FALSE)
{
// this block working when get error
$this->load->view('myform');
}
else
{ // when success
$this->load->view('formsuccess');
}
}
check official documentation here
Use this code on your view file (where your form was submitted) <?php echo validation_errors(); ?> it return your errors
Note: this validation errors on view file only work for PHP version <=7.3. PHP v8 not supported
I'm creating a library to add new users.
class Register
{
private $CI;
public function add_new_user()
{
$CI =& get_instance();
$CI->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($CI->form_validation->run() == TRUE)
{
$email = $_POST['email'];
$insert_data = array('email' => $email);
$CI->new_data->add_user($insert_data);
}
}
private function is_email_exist($email)
{
$CI =& get_instance();
$email_result = '';
$query = $CI->check->find_email($email);
foreach ($query->result_array() as $row)
{
$email_result = $row['email'];
}
if ($email_result == $email)
{
$CI->form_validation->set_message('is_email_exist', 'Such email already exist!');
return FALSE;
}
else
{
return TRUE;
}
}
}
I add form_validation and models check, new_data to the autoload. When I submit a form instead of an error (if it should be there) I get Unable to access an error message corresponding to your field name Username.(is_username_exist). What should i do to get right error?
Codeigniter form validation is for the controller so when submit form it goes there
https://www.codeigniter.com/user_guide/libraries/form_validation.html#the-controller
Controller Way
<?php
class Register extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('some_model');
}
public function index() {
$this->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($this->form_validation->run()) {
$insert_data = array(
'email' => $this->input->post('email')
);
$this->some_model->new_data->add_user($insert_data);
}
$data['title'] = 'Welcome To Codeigniter';
$this->load->view('header', $data);
$this->load->view('someview', $data);
$this->load->view('footer');
}
// Check user email
public function is_email_exist() {
// Single input check
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('user');
// If user email greater than 0
if ($query->num_rows() > 0) {
return TRUE;
} else {
$this->form_validation->set_message('is_email_exist', 'Such email already exist!');
return FALSE;
}
}
}
replace 'is_email_exist' to __FUNCTION__ work for me,
https://stackoverflow.com/a/38950446/2420302
I'm new in Codeigniter. I have a login system where user with status is 0 can't do login yet and where user with status 1 can login. I might be have a mistake in my code. So, i hope you can find where is my mistake and help me to make it right. here's my code.
My Controller
public function login() {
$this->form_validation->set_rules('no', 'No', 'required|min_length[10]|max_length[16]|integer');
$this->form_validation->set_rules('password', 'password', 'required|md5|xss_clean');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if($this->form_validation->run()== FALSE) {
$this->load->view('v_login');
}else{
$no = $this->input->post('no');
$password = $this->input->post('password');
$cek = $this->m_user->ambilPengguna($no, $password);
$status = $this->m_user->ambilStatus($no); //HERE'S THE PROBLEM
if($cek->num_rows()<> 0 && $status == '1') { //HERE'S TOO, IT WON'T CHECK THE STATUS.
$this->session->set_userdata('isLogin', TRUE);
$this->session->set_userdata('data_user',$cek->row());
redirect('c_belajar');
}else {
echo " <script>
alert('Login failed! call the administrator to activate your account');
history.go(-1);
</script>";
}
}
}
My Model
public function ambilPengguna($no, $password) {
$this->db->select('*');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$this->db->where('password', $password);
$query = $this->db->get();
return $query;
}
public function ambilStatus($no){
$this->db->select('status');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$query = $this->db->get();
return $query;
}
There's a mistake on controller. Please help me.
Alright! May I suggest a little code restructure
Controller
class YourController extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login_model');
$this->load->library('form_validation');
}
public function login()
{
if($_POST)
{
$config=array(
array(
'field' => 'no',
'label' => 'Number',
'rules' => 'trim|required',
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required',
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run()==false)
{
$data['errors']=validation_errors();
$this->load->view('login',$data);
}
else
{
$check=$this->login_model->checkUser($_POST); // you can use xss clean here filter post data
if(!$check)
{
$data['errors']='Invalid Password';
$this->load->view('login',$data);
}
elseif($check==1)
{
$data['errors']='Your account status is not active yet, Please contact Administrator';
$this->load->view('login',$data);
}
else
{
$this->session->set_uerdata($check);
redirect(base_url().'dashboard');
}
}
}
else
{
$this->load->view('login');
}
}
}
Model
class login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function checkUser($data)
{
$st=$this->db->select('*')
->from('tbl_user')
->Where('no_id', $data['no'])
->where('password', $data['password'])
->get()->result_array();// you can use row()
if(count($st)>0)
{
if($st[0]['status']==0){
return 1;
}
else
{
return $st[0];
}
}
else
{
return false;
}
}
}
Try this, this working in my project..
Your Model
public function ambilStatus(){
$this->db->where('no_id', $this->input->post('your input name'));
$query = $this->db->get($this->db->dbprefix . 'tb_user');
$ret = $query->row();
return $ret->account_status;
}
And Your Controller
$status = $this->m_user->ambilStatus();
if($status && $cek->num_rows() == 1 ) {
I am trying to develop a login panel using codeigniter but I am unable to do so as I believe my concept is not so clear yet though or Am i doing something wrong please help me out with this concern
Controllers>admin.php
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run()) {
$this->verify_user->can_log_in();
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_id') == true) {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
models>verify_users.php
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
$this->load->view('admin/login', $data);
}
}
}
The thing is happening when I login with correct username and password it redirects me back to login.php when I put the model script within the verify function it runs perfectly
Please help me out with this
This is the closest possible fix to your way of implementation.
You need to consider reading more about MVC.
Try replace your controller with this:
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run() && $this->verify_user->can_log_in()) {
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_in') == "1") {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
}
And your model with this:
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => "1"
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
return false;
}
}
}
Check this
class admin extends CI_Controller {
^// this should be Admin
IN model
else {
//$data['message'] = 'Incorrect username/password';
//$this->load->view('admin/login', $data);
//dont load view in model
return false;
}
In controller
if($this->form_validation->run()) {
$res = $this->verify_user->can_log_in();
if($res)
redirect('admin/dashboard');
else
redirect('admin/login');
} else {
$this->load->view('admin/login');
}
Fixing these 3 errors should help you.
I created a login system but every time I setup an if statement it loops back to the login page when I enter correct password. I need the index function in the controller, the list_employee function and View_employee function to redirect user to login page if they access it directly but if they enter correct password allow them to go to it.
user_authentication controller
<?php
session_start(); //we need to start session in order to access it through CI
Class User_Authentication extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
// Load session library
$this->load->library('session');
// Load database
$this->load->model('login_database');
}
// Show login page
public function user_login_show() {
$this->load->view('login_form');
}
// Show registration page
public function user_registration_show() {
$this->load->view('registration_form');
}
// Validate and store registration data in database
public function new_user_registration() {
// Check validation for user input in SignUp form
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_form');
} else {
$data = array(
'name' => $this->input->post('name'),
'user_name' => $this->input->post('username'),
'user_email' => $this->input->post('email_value'),
'user_password' => $this->input->post('password')
);
$result = $this->login_database->registration_insert($data) ;
if ($result == TRUE) {
$data['message_display'] = 'Registration Successfully !';
$this->load->view('login_form', $data);
} else {
$data['message_display'] = 'Username already exist!';
$this->load->view('registration_form', $data);
}
}
}
// Check for user login process
public function user_login_process() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if($result == TRUE){
$sess_array = array(
'username' => $this->input->post('username')
);
// Add user data in session
$this->session->set_userdata('logged_in', $sess_array);
$result = $this->login_database->read_user_information($sess_array);
if($result != false){
$data = array(
'name' =>$result[0]->name,
'username' =>$result[0]->user_name,
'email' =>$result[0]->user_email,
'password' =>$result[0]->user_password
);
redirect('employee');
}
}else{
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}
// Logout from admin page
public function logout() {
// Removing session data
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $data);
}
}
?>
employee controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Employee extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login/employee_model');
}
//Shows the dashboard
public function index()
{
$this->load->view('header');
$this->load->view('employee');
$this->load->view('login/footer');
}
//Insert the employee
public function insert_employee()
{
$data=array('name'=>$this->input->post('name'),
'LanId'=>$this->input->post('LanId'),
'reason'=>$this->input->post('reason'),
'PepNumber'=>$this->input->post('PepNumber'),
'Employee_Number'=>$this->input->post('Employee_Number'),
'department'=>$this->input->post('department'),
'status'=>1);
//print_r($data);
$result=$this->employee_model->insert_employee($data);
if($result==true)
{
$this->session->set_flashdata('msg',"Employee Records Added Successfully");
redirect('employee');
}
else
{
$this->session->set_flashdata('msg1',"Employee Records Added Failed");
redirect('employee');
}
}
//List of Employees
public function list_employees()
{
$data['employee']=$this->employee_model->get_employee();
$this->load->view('header');
$this->load->view('list_of_employees',$data);
$this->load->view('login/footer');
}
//List of Employees
public function viewlist_employees()
{
$data['employee']=$this->employee_model->get_employee();
$this->load->view('header');
$this->load->view('viewlist_of_employees',$data);
$this->load->view('login/footer');
}
public function delete_employee()
{
$id=$this->input->post('id');
$data=array('status'=>0);
$result=$this->employee_model->delete_employee($id,$data);
if($result==true)
{
$this->session->set_flashdata('msg1',"Deleted Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"Employee Records Deletion Failed");
redirect('employee/list_employees');
}
}
public function edit_employee()
{
$id=$this->uri->segment(3);
$data['employee']=$this->employee_model->edit_employee($id);
$this->load->view('header',$data);
$this->load->view('edit_employee');
}
public function update_employee()
{
$id=$this->input->post('id');
$data=array('name'=>$this->input->post('name'),
'LanID'=>$this->input->post('LanID'),
'reason'=>$this->input->post('reason'),
'PepNumber'=>$this->input->post('PepNumber'),
'Employee_Number'=>$this->input->post('Employee_Number'),
'department'=>$this->input->post('department'),
'status'=>1);
$result=$this->employee_model->update_employee($data,$id);
if($result==true)
{
$this->session->set_flashdata('msg',"Employee Records Updated Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"No changes Made in Employee Records");
redirect('employee/list_employees');
}
}
}
?>
login_database model
<?php
Class Login_Database extends CI_Model {
// Insert registration data in database
public function registration_insert($data) {
// Query to check whether username already exist or not
$condition = "user_name =" . "'" . $data['user_name'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 0) {
// Query to insert data in database
$this->db->insert('user_login', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
return false;
}
}
// Read data using username and password
public function login($data) {
$condition = "user_name =" . "'" . $data['username'] . "' AND " . "user_password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}
// Read data from database to show data in admin page
public function read_user_information($sess_array) {
$condition = "user_name =" . "'" . $sess_array['username'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
}
?>
employee_model
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
public function get_employee()
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
}
add if statement with logged_in and a redirect to login form if it
is incorrect
public function index()
{
if($this->session->userdata('logged_in'))
{
$this->load->view('header');
$this->load->view('employee');
$this->load->view('login/footer');
}else{
redirect('user_authentication/user_login_show');
}
}
Best Practice is to add the check in the constructor of your controller in CI.
here is the example of mine.
public function __construct() {
parent::__construct();
if (!$this->session->userdata('user_data')) {
return redirect('login');
}
$this->load->model('customer_model');
}
you can add the else statement to redirect to the dashboard or what the resulting page if user is logged in.
Add this line of code to your constructors:
$this->load->library('session');
This will help you.
public function login()
{
$this->load->view('login');
if (isset($_POST['login']))
{
$emailid = $this->input->post('emailid');
$password = $this->input->post('password');
$this->load->model('main_model');
if($this->main_model->can_login('$emailid','$Password'))
{
$session_data = array(
'emailid' => $emailid,
'password' => $password,
'iss_logged_in' => 1
);
$this->session->set_userdata($session_data);
redirect(base_url().'index.php/Hello_cnt/');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url().'index.php/Hello_cnt/login');
}
}
}