I am having problems with my college assignments. I'm learning to make a update function trough codeigniter, then I got an error :
Missing argument 1 for Account::update()
When I press "Submit" button.
Controller :
class Account extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('cpanel/account/account_model');
}
public function update($nim) {
$this->form_validation->set_rules('nim', 'nim', 'required');
$this->form_validation->set_rules('nama', 'nama', 'required');
$this->form_validation->set_rules('sandi', 'sandi', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('telp', 'telp', 'required');
if ($this->form_validation->run() === FALSE) {
$data['akun'] = $this->account_model->detail();
$data['detail'] = $this->account_model->detail($nim);
$data = array('title'=> 'Mengubah Data Akun : '.$data['detail']['ortu_nama'], 'akun'=> $this->account_model->detail(), 'detail'=> $this->account_model->detail($nim), 'isi'=>'cpanel/account/account_edit_view');
$this->load->view('cpanel/layout/wrapper',$data);
} else {
$data = array(
'ortu_nim_mhs' => $this->input->post('nim'),
'ortu_nama' => $this->input->post('nama'),
'ortu_email' => $this->input->post('email'),
'ortu_telp' => $this->input->post('telp')
);
$this->account_model->update($data);
$konten = array('title'=>'Perubahan Data Sukses', 'isi'=>'cpanel/account/success_view');
$this->load->view('cpanel/layout/wrapper', $konten);
}
}}
Model :
class Account_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function update($data) {
$this->db->where('ortu_nim_mhs', $data['ortu_nim_mhs']);
return $this->db->update('user_ortu', $data);
}
public function detail($nim = FALSE) {
if ($nim === FALSE) {
$query = $this->db->get('user_ortu');
return $query->result_array();
}
$query = $this->db->get_where('user_ortu', array('ortu_nim_mhs'=>$nim));
return $query->row_array();
}}
Try $data = array(); in model function
public function update($data = array()) {
$this->db->where('ortu_nim_mhs', $data['ortu_nim_mhs']);
return $this->db->update('user_ortu', $data);
}
In your above code if you are submitting form with POST method than function argument is not required. Please check below.
public function update() {
// Your code
}
But in case if you submitting form with get method than you need to set data for validation first, like given below.
public function update() {
$this->form_validation->set_data($this->input->get());
$this->form_validation->set_rules('nim', 'nim', 'required');
$this->form_validation->set_rules('nama', 'nama', 'required');
$this->form_validation->set_rules('sandi', 'sandi', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('telp', 'telp', 'required');
// Your other code
}
This will help you. Let me know if it not works.
Related
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){
....
}
i have a problem that i can edit all profiles as user. but i want, that i only can edit my profile. Maybe you can help me. The code is working if i want to edit my profile like
domain/member/users/manage/1
but i can edit /member/users/manage/2 too and thats the problem.
Controller Users.php
public function manage($id = NULL) {
if($this->session->userdata('type') == 'user') {
$data = array();
if ($id) {
$this->{$this->model}->{$this->_primary_key} = $id;
$data['item'] = $this->{$this->model}->get();
if (!$data['item'])
show_404();
} else {
$data['item'] = new Std();
}
$this->load->library("form_validation");
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->{$this->model}->custom_select = 'users.*, teams.title as teams';
$this->{$this->model}->joins = array( 'teams' => array('teams.teams_id = users.teams_id', 'inner'));
$this->form_validation->set_rules('firstname', 'firstname', 'trim|required');
$this->form_validation->set_rules('lastname', 'lastname', 'trim');
$this->form_validation->set_rules('sex', 'sex', 'trim');
$this->form_validation->set_rules("image3", 'image3', "trim|callback_image3[$id]");
$this->form_validation->set_rules("email", 'Email', "trim|required|valid_email");
if ($id)
$this->form_validation->set_rules('password', 'Password', 'trim');
else
$this->form_validation->set_rules('password', 'Password', 'trim');
if ($this->form_validation->run() == FALSE)
$this->load->view($this->module . '/manage', $data);
else {
$this->users_model->username = $this->input->post('username');
$this->users_model->email = $this->input->post('email');
$this->users_model->firstname = $this->input->post('firstname');
$this->users_model->lastname = $this->input->post('lastname');
$this->users_model->sex = $this->input->post('sex');
if (strlen($this->input->post('password')) > 0)
$this->{$this->model}->password = md5($this->input->post('password'));
$this->{$this->model}->save();
redirect('member/' . $this->module);
}
}else{
exit('Hacking Attempt: Get out of the system ..!');
}
}
Users_model.php
<?php
class Users_model extends CI_model
{
public $_table = 'users';
public $_primary_keys = array('user_id');
}
First One you Must pass The user_id in session
Then Need domain/member/users/manage/1
You redirect to ur profile ...
If Users controller having function manage
class Users extends CI_Controller
{
public function manage($userid)
{
#checking the with session value
if(($this->session->userdata('user_id')!=$userid)):
#if not satisfied means redirect another page
redirect("Another page");
endif;
}
}
I am setting the form rules in my users model and im having a hard time to reach the is_unique_email callback function. It never enters the function. All seems to be working fine despite that!
Can anyone help me sorting this issue?
Users Model:
public function form_rules()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('nome', 'Nome', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_is_unique_email');
$this->form_validation->set_rules('password', 'Password', 'required|matches[password_repeat]');
$this->form_validation->set_rules('password_repeat', 'Repeat Password', 'required');
if ($this->form_validation->run() == FALSE)
{
return false;
}
else
{
return true;
}
}
public function is_unique_email()
{
die("asadadaa");
}
My controller
public function insert()
{
if ($this->user_model->form_rules() == FALSE) {
$this->index();
} else {
echo "The form was validated!";
}
}
i made a controller name of user.php i test complete project on local host it works fine but when i upload online on website it shows error , i submit the form then form load controller called user.php and error on the first line its called parse error i check but still the same will you please suggest.
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_signup');
}
else
{
$this->load->model('Users_model');
if($this->input->post("language")=="ar")
{
$this->load->view('ar_thanks');
}
else
{
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member())
{
$this->load->view('ar_thanks');
}
}
}
}
Please format your code and use IDE. And you will not have these errors. Your formatted code:
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->load->view('ar_signup');
} else {
$this->load->model('Users_model');
if($this->input->post("language")=="ar") {
$this->load->view('ar_thanks');
} else {
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member()) {
$this->load->view('ar_thanks');
}
}
}
}
I've been debugging this code but still not successful. Can anyone help me out please?
class Membership_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
I kept on getting an fatal error on the line
$this->db->where('username',$this->input->post('username'));
this is the controller/login.php
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
The database probably is not being initialized properly before validate is called.
Looks like you're not connected to your DB. Make sure you are connecting to your database.
You can either connect automatically everytime your script runs or connect to the DB manually. Look at the CI guides for your connection options : http://codeigniter.com/user_guide/database/connecting.html
Could it be that the load->model( 'membership_model' ) should receive the name of a class, and that that name is case sensitive? You should probably check the return value of the API functions...