1

我是 Code Igniter 的新手,我正在尝试从表单更新用户数据。插入、显示和删除数据工作正常,但我无法让更新功能工作。

数据保存在两个表中:usersusers_info

我的模型:

function getuser($id) {
  $this->db->join('users_info','users.user_id = users_info.user');

  $query = $this->db->get_where('users', array('user_id'=> $id));

  return $query->result_array();
 }

function edituser($id) {

  $update_user = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),     
    'email' => $this->input->post('email'),
    'level' => $this->input->post('role')                                       
  );

  $this->db->where('user_id', $id);
  $this->db->join('users_info','users.user_id = users_info.user' );
  $this->db->update('users', $update_user);

  $affected_rows1 = $this->db->affected_rows() ? TRUE : FALSE;

  $update_info = array(
    'name' => $this->input->post('first_name'),
    'surname' => $this->input->post('surname'),
    'address' => $this->input->post('address'),         
    'city' => $this->input->post('city'),
    'phone' => $this->input->post('phone'),
    'mobile' => $this->input->post('mobile'),
    'am' => $this->input->post('am'),           
    'vat' => $this->input->post('vat')                              
  );

  $this->db->where('user', $id);
  $this->db->update('users_info', $update_info); 
  $affected_rows2 = $this->db->affected_rows() ? TRUE : FALSE;

  return ($affected_rows1 && $affected_rows2) ? TRUE : FALSE;

}

我的控制器:

function edituser() {
  if ($this->session->userdata('is_logged_in')) {
    $this->load->helper('form');  
    $this->load->helper('html');   
    $this->load->model('users_model');
    $id=$this->uri->segment(3);
    $this->users_model->edituser($id);  

  if ((int)$id > 0) {

    $query = $this->users_model->getuser($id);   

    $data['fid']['value']         = $id;
    $data['fusername']['value']   = $query['username'];
    $data['fpassword']['value']   = $query['password'];
    $data['femail']['value']      = $query['email'];
    $data['frole']['value']       = $query['role'];
    $data['ffirst_name']['value'] = $query['first_name'];
    $data['fsurname']['value']    = $query['surname'];
    $data['faddress']['value']    = $query['address'];
    $data['fcity']['value']       = $query['city'];
    $data['fphone']['value']      = $query['phone'];
    $data['fmobile']['value']     = $query['mobile'];
    $data['fam']['value']         = $query['am'];
    $data['fvat']['value']        = $query['vat'];
  }

  $data['main_content'] = 'pages/edit_user';
  $this->load->view('templates/template', $data);  

  } else {
    redirect('login/index');
  }
}

我的观点

<div class="form-grid">

  <?php
    $attributes= array('class' => 'leftLabel', 'id' => 'form1');
    echo form_open('users/edituser',  $attributes);
  ?>
  <? echo form_hidden('id',$fid['value']); ?>

  <ul>
    <li>
      <label class="fldTitle">Name<abbr title="Required Field" class="require">*</abbr></label>

      <div class ="fieldwrap">
        <span class="fldcol left">
        <input name="ffirst_name" type="text" tabindex="13" value="<?php echo set_value('ffirst_name',$ffirst_name); ?>"class="full">
        <label class="fldLabel">First Name</label>
        <?php echo form_error('first_name');   ?>   
        </span><span class="fldcol right">
        <input name="fsurname" type="text" tabindex="14" value="<?php echo set_value('fsurname'); ?>" class="full">
        <label class="fldLabel">Surname</label>
        <?php echo form_error('surname');   ?>   
        </span>
      </div>
    </li>
    <li>
      <label class="fldTitle">Login info<abbr title="Required Field" class="require">*</abbr></label>
      <div class ="fieldwrap">

        <span class="fldcol left">
          <input name="username" type="text" tabindex="13"  class="full">
          <label class="fldLabel">Username</label>
          <?php echo form_error('username');   ?>   
        </span>

        <span class="fldcol right">

        <select name="role" tabindex="12" id="combobox" value="<?php echo set_value('role'); ?>">
          <option value="first"<?php echo set_select('role', 'first', TRUE); ?>>First</option>
          <option value="second"<?php echo set_select('role', 'second'); ?>>Second</option>
          <option value="third"<?php echo set_select('role', 'third'); ?>>Third</option>
        </select>

        <label class="fldLabel">Choose user role</label>

        <?php echo form_error('role');   ?> 

        </span><span class="fldcol left">

        <input name="password" type="password" tabindex="13" class="full">
        <label class="fldLabel">Password</label>
        <?php echo form_error('password');   ?> 

        </span> 

        <span class="fldcol right">
          <input name="password2" type="password" tabindex="13" class="full">
          <label class="fldLabel">Comfirm Password</label>
          <?php echo form_error('password2');   ?>   
        </span>
      </div>
    </li>
    <li class="cmplxFld error">
      <label class="fldTitle">User info<abbr title="Required Field" class="require">*</abbr></label>
      <div class ="fieldwrap">
        <span class="full">
        <input name="address" type="text" tabindex="15" value="<?php echo set_value('address'); ?>" class="full">
        <label class="fldLabel">Street Address</label>
        <?php echo form_error('address');   ?>   
        </span>
        <span class="fldcol left">
        <input name="city" type="text" tabindex="17" value="<?php echo set_value('city'); ?>" class="full">
         <label class="fldLabel">City</label>
        <?php echo form_error('city');   ?> 
        </span>
         <span class="fldcol right">
        <input name="email" type="text" tabindex="18" value="<?php echo set_value('email'); ?>" class="full">
        <label class="fldLabel">email</label>
        <?php echo form_error('email');   ?>   
        </span>
        </span>
        <span class="fldcol left">
        <input name="phone" type="text" tabindex="17" value="<?php echo set_value('phone'); ?>"  class="full">
        <label class="fldLabel">Phone</label>
        <?php echo form_error('phone');   ?>   
        </span>
        <span class="fldcol right">
        <input name="mobile" type="text" tabindex="18" value="<?php echo set_value('mobile'); ?>" class="full">
        <label class="fldLabel">Mobile</label>
        <?php echo form_error('mobile');   ?>   
        </span>
        </span>
        <span class="fldcol left">
        <input name="am" type="text" tabindex="17" value="<?php echo set_value('am'); ?>" class="full">
        <label class="fldLabel">AM</label>
        <?php echo form_error('am');   ?>   
        </span>
        <span class="fldcol right">
        <input name="vat" type="text" tabindex="19" value="<?php echo set_value('vat'); ?>" class="full">
        <label class="fldLabel">VAT</label>
        <?php echo form_error('vat');   ?>   
        </span>
      </div>
    </li>
    <li class="buttons bottom-round noboder">
      <div class ="fieldwrap">
        <input name="submit" type="submit" value="Submit" class="submit-button">
      </div>
    </li>
  </ul>

</form>
</div>

谁能告诉我我的代码有什么问题?

4

2 回答 2

1

你需要做的是传递第三个参数

$this->db->update('users', $update_user);

所以它看起来像这样

$this->db->update('users', $update_user,array('field_name'=>$id));

第三个参数采用字符串数组作为查询的 where 部分。它生成这个查询:

UPDATE users SET (...) WHERE field_name = content_of_$id_variable

编辑:我重新阅读了您的问题,并注意到您在第一次更新时添加了一个 join 子句。这段代码应该可以工作。

模型 :

function edituser($id) {

    $this->db->trans_start();

    $update_user = array(
        'username' => $this->input->post('username'),
        'password' => md5($this->input->post('password')),     
        'email' => $this->input->post('email'),
        'level' => $this->input->post('role')                                       
    );

    $this->db->update('users', $update_user,array('user_id'=>$id));

    $update_info = array(
        'name' => $this->input->post('first_name'),
        'surname' => $this->input->post('surname'),
        'address' => $this->input->post('address'),         
        'city' => $this->input->post('city'),
        'phone' => $this->input->post('phone'),
        'mobile' => $this->input->post('mobile'),
        'am' => $this->input->post('am'),           
        'vat' => $this->input->post('vat')                              
    ); 

    $this->db->update('users_info', $update_info,array('user',$id)); 

    $status = $this->db->trans_status();

    $this->db->trans_complete();

    return $status;
}

告诉我进展如何。

于 2012-09-18T19:42:17.467 回答
0

您的 CI 语法看起来是正确的。

我会检查

  1. 结果查询是否正确。您可以使用 last_query() 函数来查看它。http://codeigniter.com/user_guide/database/helpers.html
  2. 所需参数是否正确通过您的表单发布
于 2012-09-18T20:26:47.700 回答