Codeigniter更新记录不会保存在DB中

时间:2013-05-11 09:59:47

标签: php mysql database codeigniter

我在使用Codeigniter框架更新记录时遇到了一些麻烦。我正在使用MVC模式和活动记录。

该代码用于更新用户个人资料。

没有错误但它不会保存到我的数据库中。可能的解决办法是什么?

模型(model_users.php)

public function profile_update()
{

    $user_profile = $this->db->get('ij_users');

    if($user_profile) {

        $row = $user_profile->row();

        $data = array(

        'user_fname' => $row->user_fname,
        'user_lname' => $row->user_lname,
        'user_pass' => $row->user_pass,
        'user_company' => $row->user_company

        );

        $user_update = $this->db->update('ij_users', $data);
    }

    if($user_update) {

        return true;

    }

    return false;
}

控制器(members.php)

public function validate()
{

    $this->load->library('form_validation');

    $this->form_validation->set_rules('user_fname', 'First name', 'trim|xss_clean');
    $this->form_validation->set_rules('user_lname', 'Last name', 'trim|xss_clean');
    $this->form_validation->set_rules('user_pass', 'Password', 'required|trim|xss_clean');
    $this->form_validation->set_rules('user_re_pass', 'Re-type Password', 'required|trim|xss_clean|matches[user_pass]');
    $this->form_validation->set_rules('user_company', 'Company name', 'trim|xss_clean');

    if($this->form_validation->run()) {

        $this->load->model('model_users');

        if($this->model_users->profile_update()) {

            //update database
            //show successfull message
            echo "Successfully saved!";

        } else {

            //show error updating message
            echo "Problem adding to database 2.";

        }

    } else {

        //show error message
        echo "Problem adding to database.";
    }
}

查看(profile.php)

<div class="form-signin">
<?php echo form_open('members/validate'); ?>

<?php
  echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button><h5>All fields are required!</h5>';
  echo validation_errors();
  echo '</div>';

  echo "<p><strong>Email address </strong></p>";
  echo form_input('user_email', $this->input->post('user_email'), 'disabled');

  echo "<p><strong>First name</strong></p>";
  echo form_input('user_fname', $this->input->post('user_fname'));

  echo "<p><strong>Last name</strong></p>";
  echo form_input('user_lname', $this->input->post('user_lname'));

  echo "<p><strong>Password </strong></p>";
  echo form_password('user_pass');


  echo "<p><strong>Re-type password </strong></p>";
  echo form_password('user_re_pass');

  echo "<p><strong>Company name</strong></p>";
  echo form_input('user_company', $this->input->post('user_company'));

  echo "<p>";
  echo form_submit('save_submit', 'Save changes', 'class="btn btn-primary"');
  ?>
  <a href="<?php echo base_url() . "members" ?>" class="btn btn-link btn-small">Cancel</a>
  <?php
  echo "</p>";

  echo form_close();
?>

2 个答案:

答案 0 :(得分:1)

通过查看您的模型代码,我看不到任何主键ID,这是记录需要更新的地方。因此,请将ID添加到模型文件中的$ data数组中。

 $data = array(
    'user_id/id' => $row->id //********* add id here *********
    'user_fname' => $row->user_fname,
    'user_lname' => $row->user_lname,
    'user_pass' => $row->user_pass,
    'user_company' => $row->user_company

    );

答案 1 :(得分:0)

可能有多种原因无法发挥作用。但是,我在你的模型中看到了一些不完全正确的东西。

除非你在users表中只有一行(即使那时不确定这是否有效),你应该使用:

$user_profile = $this->db->get_where('ij_users',array('id'=>$this->session->usedata('id')));

/*supposing you have id as PK in the table, an active session for that id, and want to get the logged in profile user data */