Codeigniter - 更新数据库功能不起作用(尝试编辑配置文件)

时间:2015-02-22 17:33:47

标签: php mysql codeigniter

我有一个页面,用户可以更改其用户帐户信息,它将从数据库更新。数据库表不受影响。什么都没发生。它甚至不会向我显示表单验证,例如"错误:此字段是必需的"或"错误:请输入有效的电子邮件"

我无法让这个工作,因为我得到3个错误:

  

遇到PHP错误

     

严重性:注意

     

消息:使用未定义的常量FLASE - 假设' FLASE'

     

文件名:controllers / profile.php

     

行号:45

  

遇到PHP错误

     

严重性:注意

     

消息:未定义属性:Profile :: $ model_profile

     

文件名:controllers / profile.php

     

行号:52

最后

  

致命错误:在第52行的/Applications/XAMPP/xamppfiles/htdocs/dayone/application/controllers/profile.php中调用null上的成员函数update()

我的profile.php控制器:

public function update_profile()
{

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

    //rules to become a registered user

    $this->form_validation->set_rules('first_name', 'First Name', 'required|trim|min_length[3]|max_length[20]|xss_clean|alpha_numeric');
    $this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|min_length[3]|max_length[20]|xss_clean|alpha_numeric');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|min_length[6]|max_length[50]|valid_email|is_unique[users.email]|xss_clean');

    if ($this->form_validation->run() == FLASE) {
        //user didin't validate, send back to login form
        $this->load->view('profile/includes/header');
        $this->load->view('profile/profile_settings');
    }
    else {

        $result = $this->model_profile->update();
        //successful update

        if ($result) {
            //$this->load->view
            $this->load->view('profile/includes/header');
        $this->load->view('profile/profile_body');
        }
    }

}

用户编辑其信息的表单页面:

<!-- Forms -->
            <?php
           $attributes = array('class' => 'pi-row pi-grid-small-margin', 'id' => 'contact-form');
            echo form_open('profile/update_profile', $attributes); 
            ?>


                <div class="pi-row pi-grid-small-margins">

                    <!-- Col 6 -->
                    <div class="pi-col-sm-6">

                        <!-- First name form -->
                        <div class="form-group">
                            <label for="first_name">First name</label>

                            <div class="pi-input-with-icon">
                                <div class="pi-input-icon"><i class="icon-user"></i></div>
                                <input type="text" name="first_name" value="<?php echo $first_name;?>"; class="form-control" id="first_name" placeholder="<?php echo $first_name;?>">
                            </div>
                        </div>
                        <!-- End first name form -->

                    </div>
                    <!-- End col 6 -->

                    <!-- Col 6 -->
                    <div class="pi-col-sm-6">
                    <!-- Last name form -->
                        <div class="form-group">
                            <label for="last_name">Last name</label>

                            <div class="pi-input-with-icon">
                                <div class="pi-input-icon"><i class="icon-user"></i></div>
                                <input type="text" name="last_name" value="<?php echo $last_name;?>" class="form-control" id="last_name" placeholder="<?php echo $last_name;?>">
                            </div>
                        </div>
                        <!-- End Last name form -->

                    </div>
                    <!-- End col 6 -->

                </div>


                <!-- Email form -->
                <div class="form-group">
                    <label for="exampleInputName-3">Email Address</label>

                    <div class="pi-input-with-icon">
                        <div class="pi-input-icon"><i class="icon-mail"></i></div>
                        <input type="text" name="email" value="<?php echo $email;?>" class="form-control" id="email" placeholder="value="<?php echo $email;?>"">
                    </div>
                </div>
                <!-- End Email form -->

                <!-- Username form -->
                <div class="form-group">
                    <label for="exampleInputCompanyname-3">Username <font color="#aca3a3"> (Cannot be changed)</font></label>

                    <div class="pi-input-with-icon">
                        <div class="pi-input-icon"><i class="icon-users"></i></div>
                        <input type="text" disabled name="username" class="form-control" value="<?php echo $username;?>" id="username" placeholder="<?php echo $username;?>">
                    </div>
                </div>
                <!-- Username form -->


                <hr class="pi-divider-gap-10">

                <!-- Submit button -->
                <p>
                    <button type="submit" class="btn pi-btn-green">
                        <i class="icon-pencil pi-icon-left"></i>Save
                    </button>
                   <!-- Button to cancel -->
                    <a class="btn pi-btn-red icon-cancel pi-icon-left" href="<?php echo base_url();?>profile">Cancel</a>
                <!-- End submit button -->

             <?php echo form_close(); ?>

最后,我的model_profile.php模型:

    public function update() {


    $first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $email = $this->input->post('email');

    $sql = "UPDATE users SET first_name = '{$first_name}', last_name = '{$last_name}', email = '{$email}' WHERE uid = $uid LIMIT 1";
    $this->db->query($sql);

    if ($this->db->affected_rows() === 1) {

        return true;
    } else {
        return false;
    } 
}

2 个答案:

答案 0 :(得分:1)

第一个错误,你有一个错字:

if ($this->form_validation->run() == FLASE)

而不是

if ($this->form_validation->run() == FALSE)

第二和第三个错误,您必须在使用之前加载模型:

$this->load->model("model_profile");
$result = $this->model_profile->update();    

答案 1 :(得分:0)

您可以像这样调用模态函数

       $this->load->model('model_profile');
       $this->model_profile->update;