I have a simple crud application with Codeigniter. I’m still trying to learn the framework. But I’m getting an error in the Update Controller and can’t seem to find it.
The Three Errors I’m getting are:
Severity: Warning
Message: Missing argument 1 for Customers::update_customer()
Filename: controllers/customers.php
Line Number: 67
Severity: Notice
Message: Undefined variable: id
Filename: controllers/customers.php
Line Number: 94
And this last one is a result of the form action.
Severity: Notice
Message: Undefined index: id
Filename: views/edit_customer.php
Line Number: 16
” method=”post”>
CONTROLLER:
function update_customer($id){
$data['success']=0;
if($_POST){
$data_customer=array(
'first_name'=>$_POST['first_name'],
'last_name'=>$_POST['last_name'],
'phone'=>$_POST['phone'],
'email'=>$_POST['email'],
'website'=>$_POST['website'],
'business_name'=>$_POST['business_name'],
'business_add'=>$_POST['business_add'],
'business_cityState'=>$_POST['business_cityState'],
'cc_type'=>$_POST['cc_type'],
'cc_number'=>$_POST['cc_number'],
'cc_exp'=>$_POST['cc_exp'],
'cc_cvd'=>$_POST['cc_cvd'],
'billing_add'=>$_POST['billing_add'],
'billing_zip'=>$_POST['billing_zip'],
'package'=>$_POST['package'],
'assigned_zip_code'=>$_POST['assigned_zip_code'],
'active'=>1
);
$this->customer->update_customer($id,$data);
$data['success']=1;
}
$data['customer']=$this->customer->get_customer($id);
$this->load->view('header');
$this->load->view('edit_customer',$data);
$this->load->view('footer');
}
MODEL:
function update_customer($id, $data){
$this->where('id', $id);
$this->db->update('customers', $data);
}
TOP OF THE VIEW:
<?php if($success==1){ ?>
<div class="successAlert">This customer has been updated.</div>
<? } ?>
<div class="mainForm">
<?php echo validation_errors(); ?>
<form action="<?=base_url()?>customers/update_customer/<?=$customer['id']?>" method="post">
<label for="first_name">First Name:</label>
<input type="input" name="first_name" value="" /><br />
<hr>
1. You’re cloning an array key for key: bad
Why bother constructing the
$data_customerarray like this? The end result looks identical to$_POSTanyway with the exception of the added'active'key.If you really want to clone the array, just write:
This is, of course, a lot more resistant to changes in the values you are submitting to the server too. If you decide to send, for instance, the middle name in the future, it requires zero maintenance.
2.
if($_POST)…if($_POST)will always returnTRUEbecause$_POSTis a global, theifwill always execute. You’d be better to use CodeIgniter’s$this->input->post(), which returnsFALSEif a the user did not submit post data.3. Your Errors
You need to pass an
idinto theupdate_customer()function. This can be accomplished by sending the POST request tohttp://localhost/your_app/customers/update_customer/5, as an example.Or, you could attach the id as a hidden value in the form using
<input type='hidden' name='id' value='5'/>and then retrieve it in the$this->input->post()array as well and remove the$idparameter fromupdate_customer($id).That will clear up your first two errors.
The last error is occurring in your view because
$customerdoes not contain an index ofid. Inspect the response from$this->customer->get_customer($id)usingvar_dump()to find out why.