too few arguments for function error (updating data) CodeIgniter - php

there's an error appearing in my code for update. too few argument for function.
I searched the net and I'm not sure if I'm passing the id.
Controller:
public function update()
{
if($this->Admin_model->update($this->input->post(null, true))){
$this->session->set_flashdata('flash_msg', ['message' => 'User updated successfully', 'color' => 'green']);
} else {
$this->session->set_flashdata('flash_msg', ['message' => 'Error updating user.', 'color' => 'red']);
}
$this->admin_redirect('cms');
}
Model:
public function update($id, $data)
{
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
return $this->db->update($this->table, $data);
}

As #WILLIAM, stated in a comment, your update(...) method expects 2 parameters yet one is passed.
Change your update(...) method to this:
public function update($data)
{
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
$this->db->where('id', $data['id']);
return $this->db->update($this->table, $data);
}
This assumes that 'id' is part of your HTML form.i.e:
<input type="hidden" name="id" value="<?php echo $id ?>">

You need to call your model update function with 2 params in the controller code, but it seems you are calling it only with one parameter $data value is missing.
In below code:
if($this->Admin_model->update(**$this->input->post(null, true)**))
It should be called with 2 values first one should be an ID that identifies the record that you want to update in your model, and another should be the data that would be updated within that record.
Hope! you got the answer.

Related

How to pass the value from view page to controller

I am calling the controller function via ahref tag and i want to pass the current record no to the controller to update the record current value is stored in$currentID and i pass this value in input type..but i didn't get the value from the view page..i tried var_dump($id)..it shows null
Controller Code to update:
public function update($id='')
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$id=$this->input->post('update');
echo "<pre>";var_dump($id);
$dc=$this->input->post('dc');
if($dc=='c'){
$amount=$this->input->post('credit1');
}
else if ($dc=='d') {
$amount=$this->input->post('debit');
}
$data=array(
'date' =>$this->input->post('TDate'),
'code' =>$this->input->post('TName'),
'project' =>$this->input->post('TName1'),
'part' =>$this->input->post('part1'),
'part1' =>$this->input->post('part2'),
'dc'=>$this->input->post('dc'),
'amount'=>$amount,
);
$this->db->where('recno', $id);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully');
redirect('BookKeeping/daybook','refresh');
}
calling update
<a href="<?=site_url('BookKeeping/update/'.$currentID)?>" class="btn btn-info btn-"><i class="icon-new position-left">
<input type="hidden" name="update" id="id" value="<?php echo $currentID?>">
</i>Update</a>
Help me to pass the currentvalue to the controller..Thanks in advance
you are sending value in url
change this
$id=$this->input->post('update');
To
$id=$this->uri->segment('3');
remove the input field. You can't pass any input data with
<i class="icon-new position-left"></i>Update
Try this
public function update($id=''){
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$id=$id;
echo "<pre>";var_dump($id);
$dc=$this->input->post('dc');
if($dc=='c'){
$amount=$this->input->post('credit1');
}
else if ($dc=='d') {
$amount=$this->input->post('debit');
}
$data=array(
'date' =>$this->input->post('TDate'),
'code' =>$this->input->post('TName'),
'project' =>$this->input->post('TName1'),
'part' =>$this->input->post('part1'),
'part1' =>$this->input->post('part2'),
'dc'=>$this->input->post('dc'),
'amount'=>$amount,
);
$this->db->where('recno', $id);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully');
redirect('BookKeeping/daybook','refresh');
}
In your code
As I see your controller code posted in question, you haven't passed any value in variable $currentID which might be getting undefined, so please first try to var_dump your $currentID variable and check its value.
Do a test
Please try assigning a static id for the sake of functionality. Your static value will be passed to the method as an argument. Once you sure about static value is working, you can go ahead with finding out where is the issue in $currentID variable.
$data['currentID'] = $someValueFromDB;
$this->load->view('some_view.php',$data);
this way your value will be passed to view, make sure value is correctly passing or getting generated on view.
You are passing data in right way at controller, no problem with that.

codeigniter update method not working

I am new to codeigniter and trying to write an update function to update information in my database. I've gone through a few tutorials but for some reason my code is not working! Any tips or assistance would be greatly appreciated. I'm just testing with two fields right now, name and email, and I get a blank page when I go to my update view.
Here is my model method:
function get_by_id($id){
return $this->db->get_where('table',array('id'=>$id));
}
function update($id){
$attributes=array(
'name'=> $this->input->post('Name'),
'email'=> $this->input->post('Email')
);
return $this->db->where('id',$id)
->update('table',$attributes);
}
And here is my relevant controller code:
function edit(){
$data['row']=$this->Mymodel->get_by_id($id)->result();
$data['name']= 'Name';
$data['email']='Email';
$this->load->view('updateview', $data);
}
function update($id){
$this->load->model('Mymodel');
$this->Mymodel->update($id);
if($this->input->post()){
redirect('kittens/view/'.$id, 'refresh');
}
And here is my update view:
<?php echo form_open('kittens/update/')
echo form_hidden('id', $row[0]->id);
foreach($attributes as $field_name){
echo '<p>' . $field_name;
echo form_input($field_name, $row[0]->$field_name) . '</p>';
}
echo form_submit('', 'Update');
echo form_close();
?>
Any help would be appreciated! Not sure what specific part is giving me the trouble!
Use the following template:
Controller:
public function updateData(){
....
$data = array(
"columnName" => $columnValue
);
$whereValue = $someNumber;
$this->model_name->updateTable($data, $wherevalue);
...
}
Model:
public function updateTable($data, $whereValue){
$this->db->where('columnName', $whereValue);
$this->db->update('tableName', $data);
}
the first thing is that the model "MyModel" isn't loaded on the "edit" method in the controller.
If you're planning to use that specific model in every function of your controller you should load it in the constructor (so you don't have to load it every time).
Using$this->input->post('Name') in a model is a bad idea. You should receive that data in the controller process it (if you have to) and then send it to the method in the model.
I don't know if you can "chain" the db methods like this in codeigniter:
$this->db->where('id',$id)->update('table',$attributes);
try this:
$this->db->where('id',$id);
$this->db->update('table',$attributes);
Hope that helps
////////////model for update/////////////
public function update_customer_contacts($where, $data){
return $this->db->update('customer_contacts', $data, $where);
}
///////////////////update customer///////////////////
public function update_customer()
{
$data = array(
'company_name'=>$this->input->post('company_name'),
'address' =>$this->input->post('address'),
'telephone' =>$this->input->post('telephone'),
'fax' =>$this->input->post('fax'),
'email' =>$this->input->post('email'),
'website' =>$this->input->post('website'),
);
$res= $this->customer_model->update_customer(array('customer_id' => $this->input->post('customer_id')), $data);
;
echo json_encode($this->input->post());
}

Editing more than one field in Codeigniter

Currently, editing profile works for me. However, my problem is, only one element is successfully edited. I can echo previously saved data, and I can even type and edit each of the textboxes. Problem is, only the revision made on "profile" field is properly reflected. All the other fields remain the same.
here's what I have so far:
in controller page:
public function edit_profile()
{
//loads client profile and allows editing to be done
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->auth_model->get_user_id();
$data['client'] = $this->auth_model->get_client($id);
$this->load->view('client/edit_profile', $data);
}
public function edit_profile_submit()
{
$this->validateRole('client');
$this->load->model('auth_model');
//$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['tagline']);
$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['profile']);
//$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['billing_mode']);
redirect('client/view_profile?message=Profile updated.');
}
in model page:
public function edit_client_profile($id, $profile)
{
// allows client to edit his or her profile
$data = array(
//'tagline' => $tagline,
'profile' => $profile
//'billing_mode' => $billing_mode
);
$this->db->where('id', $id);
$this->db->update('client', $data);
}
I tried editing my controller and model page only to get errors so I commented the added lines for now instead.
I am looking forward to any possible help.
Thanks!
The error is probably because you have two undefined variables in your $data array each time you call the model. Instead of creating the array in your model, create it in your controller:
$data = array(
'tagline' => $_POST['tagline'],
'profile' => $_POST['profile'],
'billing_mode' => $_POST['billing_mode']
);
Then call the model
$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $data);
Remove the array creation from your model, change the second parameter being passed to $data:
public function edit_client_profile($id, $data)
{
$this->db->where('id', $id);
$this->db->update('client', $data);
}

Duplicate rows CodeIgniter

Im encourting a problem with my "add" function on the "subscriptions" model.
1% of the registerations is duplicated(2 or even up to 5 times) for some reason.
this is the code im using:
public function add($service, $phone, $sushi_subscription_id, $answer_id, $affiliate_id, $ip, $query, $invite_type, $invite_msg_id)
{
$this->db->set('service_id', $service->id);
$this->db->set('sushi_service_id', $service->sushi_service_id);
$this->db->set('phone', $phone);
if ($sushi_subscription_id)
{
$this->db->set('sushi_subscription_id', $sushi_subscription_id);
}
$this->db->set('answer_id', $answer_id);
if ($affiliate_id)
{
$this->db->set('affiliate_id', $affiliate_id);
}
$this->db->set('added', 'NOW()', FALSE);
$this->db->set('active', 1);
$this->db->set('ip', $ip);
$this->db->set('query', $query);
if ($invite_type)
{
$this->db->set('invite_type', $invite_type);
}
if ($invite_msg_id)
{
$this->db->set('invite_msg_id', $invite_msg_id);
}
return ($this->db->insert($this->_table_name)) ? $this->db->insert_id() : FALSE;
}
any idea how I could avoid this from happaning? the row is exactly the same.
service_id, phone, active, even the added date!
Although it is not totally irrelevant but it is very useful to build up your own generic functions or modules that serves your requirements. Here is check_duplicate function working as generic function checking duplicate data entry in DB. Here is the function
function _check_duplicate($table_name,$param)
{
$row_count=$this->db->get_where($table_name,$param)->num_rows();
if(count($row_count>0))
{
return true;
}
else
{
return false;
}
}
here is how you call the function;
if($this->_check_duplicate('table_name',array('param1'=>$param1,'param2'=>$param2)))
{
// redirect to some page
}
else
{
// do something, insert or update
}
for parameters, you can pass as many parameters as required. this function can be generic in a controller or can be part of generic module. It depends how you use it.
I think this code will help you.....
//model
function add($data){
$this->db->insert($this->table, $data);
$this->session->set_flashdata("message", "Record successfully created.");
}
//in your controller
function create(){
$data = array( 'service_id' => $this->input->post('service_id'),
'sushi_service_id' => $this->input->post('sushi_service_id'),
'phone' => $this->input->post('phone'),
'sushi_subscription_id' => $this->input->post('sushi_subscription_id'),
'answer_id' => $this->input->post('answer_id'),
'affiliate_id' => $this->input->post('affiliate_id'),
'' => $this->input->post(), // and so on and so on just like that copy and do that
//add more here
);
$this->"model name"->add($data);
}
//view
<form action="<?=site_url('controller_name/create');?>" method = "POST">//the word create is the function name and dont forget to change the controller name
<input type="text" name="service_id"/>
<input type="text" name="sushi_service_id"/>
<input type="text" name="phone"/>
//and so on and so on....just copy and do like that
<input type="submit"/>
</form>

Custom delete method doesn't call in custom datasource

I'm using a custom datasource to consume webservice.
Create, Read and Update work well but Delete doesn't works.
Here is my code calling the delete method in my controller.
public function delete($id){
$this->autoRender = false;
debug($this->Article->delete($id));
}
And here the code in my datasource
public function delete(Model $Model, $id = null) {
echo "Display a message if this method is called";
$json = $this->Http->post(CakeSession::read('Site.url') . '/webservice/delete/', array(
'id' => $id,
'apiKey' => $this->config['apiKey'],
'model' => $Model->name
));
$res = json_decode($json, true);
if (is_null($res)) {
$error = json_last_error();
throw new CakeException($error);
}
return true;
}
But when I want to delete an item, the debug(); display false.
I have no other displays.
I don't understand why my delete method isn't called correctly.
Is there something wrong in my code ?
Thanks
Let's check: you're only passing a parameter to your method:
$this->Article->delete($id)
According to the method that you created, the first parameter, which is required, is the Model. The second is $id:
public function delete(Model $Model, $id = null)
During the method you want to use both parameters. Here:
'id' => $id
And here:
'model' => $Model->name
Based on this, you need to review how this method will be called. BTW, if you want override delete() method, according the book, you need something like this: delete(int $id = null, boolean $cascade = true).

Categories