Question

I'm trying to update a MySQL table with Codeigniter.

My model code is:

function update_customer_records($updatedrow)
{
    $this->db->where('id',$this->input->post('id'));
    $this->db->update('customers',$updatedrow);
}

My view is:

    $attributes=array(
        'name'=>'updatecustomer',
        'id'=>'updatecustomer',
        );
    echo form_open('masterdata/manage_customers',$attributes);
?>

<table>
    <tr>
        <td>&nbsp;</td><td>&nbsp;</td><td>Customer Name</td><td>postalcode</td>
    <tr>

<?php if(isset($records)) : foreach ($records as $row) : ?>
    <tr>
        <td>
<?php echo anchor('masterdata/customers_updated/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>
        </td>
        <td>
            <input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
        </td>
        <td>
            <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
        </td>
        <td>
            <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
        </td>
    </tr>
<?php endforeach ; ?>
    </table>
<input type="submit" value="Update Selected">
<?php else : ?>
<h2> No Records Found</h2>
<?php endif; ?>
<?php echo form_close(); ?>

My controller is :

function manage_customers()
    {

        $data['title']="Manage Customers";

            //query model to get data results for form
            $data=array();

            if($query=$this->model_master_data->get_records()){
                $data['records']=$query;


            $this->load->view("master_data/view_master_data_header",$data);
            $this->load->view("master_data/view_master_data_nav");
            $this->load->view("master_data/view_content_master_data_manage_customers",$data);
            $this->load->view("master_data/view_master_data_footer");


            $editcustomer = $this->input->post('editcustomer');

            if(isset($editcustomer)){

                //begin outputting id of selected checkbox
                foreach ($editcustomer as $row) :
                    echo $row;

                $updatedrow=array(
                    'id'=>$row,
                    'postalcode'=>'44444'
                    );


                $this->model_master_data->update_customer_records($updatedrow);

                endforeach;
            }

I have two issues :

  1. How do I stop the foreach from running if a checkbox has not been checked.
  2. How do I pass the array to the model correctly so that the update runs?

Thanks in advance as always.

Was it helpful?

Solution

First of all I found two fields in the form with the same name and id (given below) and in one field you are setting it's value customer_name and in another you are setting postalcode.

<td>
    <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
                                --^^--
</td>
<td>
    <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
                                --^^--
</td>

So I think (probably) the name and id of the second field should be postalcode according to it's value.

Also you don't need to worry about foreach loop because the code inside the loop ll run only if there are checked check boxes on the form because unchecked check boxes won't be submitted but you can check and run the loop using following code

if( $this->input->post('editcustomer') != false )
{
    foreach ($editcustomer as $row)
    {
        // code here
    }
}

The if condition will return false if the editcustomer is not found or not submitted with the form. Also there is no id field in your form and in this case you can't use $this->input->post('id'), so if you need to check the check box id in the where clause of your model then you can use

In The controller :

if( $this->input->post('editcustomer') != false )
{
    $this->load->model('model_master_data');
    foreach ($editcustomer as $row_id)
    {
        $data = array( 'postalcode' => '44444' );
        $this->model_master_data->update_customer_records( $row_id, $data );
    }
}

I don't think you need to pass 'id'=>$row, because you probably don't wan't to update this field. Also you should use form validation to check the form input before updating the record (you may set the postcode field required to bound the user to enter a postcode).

In The Model :

function update_customer_records( $id, $data )
{
    $this->db->where( 'id', $id );
    $this->db->update( 'customers', $data );
}

So it'll do something like this (pseudo code)

update the customers table set `postalcode` = '44444' where `id` = $id

Update : I think you can also use the update_batch.

In The controller :

if( $this->input->post('editcustomer') != false )
{
    $data = array();
    foreach ($editcustomer as $row_id)
    {
        $data[] = array( 'id' => $row_id, 'postalcode' => '44444';
    }
    $this->load->model('model_master_data');
    $this->model_master_data->update_customer_records( $data );
}

In The Model :

function update_customer_records( $data )
{
    $this->db->update_batch('customers', $data, 'id');
}

OTHER TIPS

Duplicate topic with "Codeigniter update mysql table from form with CI" ?

how do I stop the foreach from running if a checkbox has not been checked.

You don't have to stop the foreach from running if a checkbox has not been checked. Because $editcustomer variable only contain checkbox checked.

How do I pass the array to the model correctly so that the update runs?

Your model is wrong. It should be:

public function update_customer_records($updatedrow)
{
    $this->db->update('customers', $updatedrow);
}

You don't need to write $this->db->where('id',$this->input->post('id')); (and it's wrong, because you can't use $this->input->post() in your model) because you already pass id in $updaterow.

Edit : I'm sorry, I read your code too quickly!

function update_customer_records($updatedrow)
{
    $this->db->where('id',$this->input->post('id'));
    $this->db->update('customers',$updatedrow);
}

...is correct. Alternatively, you can write it in a shorter way:

function update_customer_records($updatedrow)
{
    $this->db->update('customers', $updatedrow, array('id' => $this->input->post('id')));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top