简体   繁体   English

Codeigniter更新记录不会保存在数据库中

[英]Codeigniter updating records won't save in DB

I'm having some trouble updating records with the Codeigniter framework. 我在使用Codeigniter框架更新记录时遇到了一些麻烦。 I'm using the MVC pattern and active records. 我正在使用MVC模式和活动记录。

The code is for updating user profiles. 该代码用于更新用户配置文件。

No error but it won't save to my DB. 没有错误,但不会保存到我的数据库中。 What could be the possible solution for this? 有什么可能的解决方案?

Models (model_users.php) 模型(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;
}

Controller (members.php) 控制器(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.";
    }
}

View (profile.php) 查看(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();
?>

By looking at your model code, I don't see any primary key ID, which is where the record needs to be updated. 通过查看您的模型代码,我看不到任何主键ID,这是需要更新记录的位置。 So add the ID to your $data array in the model file. 因此,将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

    );

There could be multiple reasons not to work. 可能有多个原因导致无法工作。 However, I have seen something in your model that is not entirely correct. 但是,我在您的模型中看到了一些不完全正确的信息。

Unless you have just one row in the users table (and even then not sure this works), you should use: 除非在用户表中只有一行(甚至不能确定是否行),否则应使用:

$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 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM