dsrruefh12970 2013-05-11 09:59
浏览 37
已采纳

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

I'm having some trouble updating records with the Codeigniter framework. I'm using the MVC pattern and active records.

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)

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)

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)

<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条回答 默认 最新

  • dongwen9975 2013-05-11 10:35
    关注

    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 */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)