开发者

Codeigniter: sql only fires if you modify a text field in an update form

I’m having an issue with an update form. It’s a ‘client details’ form where the fields are pre-populated with the data from the DB.

Basically, the form only updates if you modify a text field, i.e. the text field submitted is a different value from before. If you don’t do this, e.g. not change anything or merely select a checkbox the form sends the variables to the model but the model doesn’t seem to execute the sql.

After testing, I know the form is sending the data, and I know the model is receiving it. It’s just that it doesn’t seem to execute the query at all. affected_rows() is zero and it returns no errors.

It’s as if CI checks cross checks the fields before executing the SQL.

But if you modify a text field it all works swimmingly-well.

My code is massive so I’ll post a summary:

Controller

if ($this->form_validation->run() == FALSE)
{
 //load views etc
}
else
{
  $title = $this->input->post('title');
  $forename = $this->input->post('forename');
  //etc...
  $this->load->model('Clients_model');

  $update_sql = $this->Clients_model->update_client(
                           $title,
                           $forename,
                           $surname,
                           $dob,
                           $email,
                           $address1,
                           $address2,
                           $town,
                           $county_state_id,
                           $address_code,
                           $country_id,
                           $alt_address1,
                           $alt_address2,
                           $alt_town,
                           $alt_county_state_id,
                           $alt_address_code,
                           $alt_country_id,
                           $use_alt_address,
                           $phone_land,
                           $phone_mobile,
                           $client_type,
                           $cancer_profile,
                           $communication_method,
                           $involvement,
                           $status,
                           $client_id
  );
  $this->session->set_flashdata('msg', $update_sql);
  redirect('/clients', 'refresh');
}  

Model

function update_client($title,
                     $forename,
                     $surname,
                     $dob,
                     $email,
                     $address1,
                     $address2,
                     $town,
                     $county_state_id,
                     $address_code,
                     $country_id,
                     $alt_address1,
                     $alt_address2,
                     $alt_town,
                     $alt_county_state_id,
                     $alt_address_code,
                     $alt_country_id,
                     $use_alt_address,
                     $phone_land,
                     $phone_mobile开发者_开发百科,
                     $client_type,
                     $cancer_profile,
                     $communication_method,
                     $involvement,
                     $status,
                     $client_id
)
{
         $this->sql = 'UPDATE client
                       SET
                       title = ?,
                       forename = ?,
                       surname = ?,
                       dob = ?,
                       email = ?,
                       address1 = ?,
                       address2 = ?,
                       town = ?,
                       county_state_id = ?,
                       address_code = ?,
                       country_id = ?,
                       alt_address1 = ?,
                       alt_address2 = ?,
                       alt_town = ?,
                       alt_county_state_id = ?,
                       alt_address_code = ?,
                       alt_country_id = ?,
                       use_alt_address = ?,
                       phone_land = ?,
                       phone_mobile = ?,
                       status = ?
                       WHERE
                       client_id = ?
                       ';
         $this->query = $this->db->query($this->sql, array(
                   $title,
                   $forename,
                   $surname,
                   $dob,
                   $email,
                   $address1,
                   $address2,
                   $town,
                   $county_state_id,
                   $address_code,
                   $country_id,
                   $alt_address1,
                   $alt_address2,
                   $alt_town,
                   $alt_county_state_id,
                   $alt_address_code,
                   $alt_country_id,
                   $use_alt_address,
                   $phone_land,
                   $phone_mobile,
                   $status,
                   $client_id
                   ));
         if ( $this->db->affected_rows() == 1)
         {
          //etc
         }
}

Many thanks!

Joe


Everything looks fine, and I never had such problems using CI...Have you tried using Active Record, just as experiment? Here I used a simpler version that doesn't pass all the params to the function but just the whole post array; in this way, you don't risk skipping or doubling some param!

Controller:

  //...
 $update_sql = $this->Clients_model->update_client($this->input->post());
 //...

Model:

function update_client($post)
{
  if(!is_array($post))
  { 
     return 'error in passing POST data';
  }
  $fields = array ('title' => $post['title'], 'forename' => $post['forname'], ....);
  $client_id = $post['client_id'];
  $this->db->where('client_id',$client_id);
  $this->db->update('client',$fields);
}    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜