Codeigniter:尝试使用codeigniter更新配置文件

时间:2015-05-14 17:58:08

标签: php codeigniter

我是codeigniter的新手,我正在尝试制作一个更新个人资料的表单。它以前有用,但现在它已经不存在了。 我在视图中改变了一些东西,它停止了工作。我把它改回原来的状态,但它已经不再适用了。我花了半天的时间试图让它再次运作,但我失败了。

也许我不知道你们能看到什么。至少我希望如此。

这是我的观点:

<?php echo form_open_multipart('Gids/do_upload');?>
    <img width="200px" src="<?php echo base_url()."uploads/".$profile[0]['image']; ?>" alt=""/>
    <label for="">Uploade new picture:</label><input type="file" name="userfile" size="20" value="128.jpg" >
    <input style="display: none" id="image" name="image" type="text" value="<?php echo "profile_picture".$_SESSION['id'].".jpg";  ?>"/>
    <label for="naam">Naam:</label><input id="naam" name="naam" type="text" value="<?php echo $profile[0]['naam'];  ?>"/>
    <label for="voornaam">Voornaam:</label><input id="voornaam" name="voornaam" type="text" value="<?php echo $profile[0]['voornaam'];  ?>"/>
    <label for="email">Gebruikersnaam:</label><input id="gebruikersnaam" name="gebruikersnaam" type="text" value="<?php echo $profile[0]['gebruikersnaam'];  ?>"/>
    <label for="email">Email:</label><input id="email" name="email" type="text" value="<?php echo $profile[0]['email'];  ?>"/>
    <label for="opleiding">Opleiding:</label><input id="opleiding" name="opleiding" type="text" value="<?php echo $profile[0]['opleiding'];  ?>"/>
    <label for="school">School:</label><input id="school" name="school" type="text" value="<?php echo $profile[0]['school'];  ?>"/>

    <label for="wachtwoord">Wachtwoord:</label><input id="wachtwoord" name="wachtwoord" type="text" />
    <label for="typeAgain">Type Opnieuw:</label><input id="typeAgain" type="text" />

    <label for="over">Over mezelf:</label><textarea name="over" id="over" cols="30" rows="10"><?php echo $profile[0]['over']?></textarea>
    <input type='text' style="display: none" name='student_id' value="<?php echo $profile[0]['student_id']?>"/>

    <button class="btn btn-default" id="changeprofile" type="submit">Wijzigingen opslaan</button>

</form>

正如您所看到的,还有一个图片上传 这是我的控制器:

function do_upload()
{
    $this->load->model("Gids_model",'',true);
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['file_name'] = 'profile_picture'.$_SESSION['id'].'.jpg';
    $config['overwrite'] = 'TRUE';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error_upload = array('error' => $this->upload->display_errors());
        redirect('Gids/datum', $error_upload);
    }
    else
    {
        $this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
        //$e = $this->input->post('student_id');

        $data_upload = array('upload_data' => $this->upload->data());
        redirect('Gids/datum', $data_upload);
    }
}

这是我的模特:

public function update_profile($id, $voornaam, $naam, $email, $wachtwoord, $opleiding, $school, $over, $image, $gebruikersnaam){
          $data = array(
                'student_id' => $id,
                'voornaam' => $voornaam,
                'naam' => $naam,
                'email' => $email,
                'wachtwoord' => $wachtwoord,
                'opleiding' => $opleiding,
                'school' => $school,
                'over' => $over,
                'image' => $image,
                'gebruikersnaam' => $gebruikersnaam
          );
         $this->db->where('student_id',  $id);
         $this->db->update('tbl_student', $data);
}
你能帮我解决一下吗? 另外,我不知道form_open_multipart('Gids/do_upload')是什么,我从教程中获得了使用codeigniter上传图像。

2 个答案:

答案 0 :(得分:0)

这是我试过的

public function do_upload() {

        $this->load->model("Gids_model",'',true);

        // load library or you can do it with autoloading
        // feature of CI
        $this->load->library('upload');

        // make sure that the folder uploads is created
        // at the root directory of the project
        $config = array(
            'upload_path' => './uploads',
            'allowed_types' => 'jpg|jpeg|JPG|JPEG|png',
            'max_size' => '1000',
            'file_name' => 'profile_picture.jpg',
            'overwrite' => true
        );

        // use initialize instead
        $this->upload->initialize($config);


        if ( ! $this->upload->do_upload('userfile'))
        {
            $error_upload = array('error' => $this->upload->display_errors());

            // you can check the errors here by  using var_dump();
            // just uncomment the the line below
            // var_dump($this->upload->display_errors());die();
            redirect('Gids/datum', $error_upload);
        }
        else
        {
            $this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
            //$e = $this->input->post('student_id');

            $data_upload = array('upload_data' => $this->upload->data());
            redirect('Gids/datum', $data_upload);
        }

    }

答案 1 :(得分:0)

我试过这个并希望它有所帮助:

控制器:

public function upload_profile() {
    $input = $this->input->post();

    $config['upload_path'] = './uploads/profile_pics/'; //path were I save the uploaded profile pics
    $config['allowed_types'] = 'gif|jpg|png'; // allowed types that is mention
    //size of the picture by default
    $config['max_size'] = '100'; 
    $config['max_width']  = '1024'; 
    $config['max_height']  = '768';
    $config['overwrite'] = true;

    $this->load->library('upload', $config);
     // display error if the picture is not on the config (sample bmp)
    if ( ! $this->upload->do_upload())
    {
        $error = $this->upload->display_errors(); // display the errors

        $data['upload_error'] = $error;
        if($this->session->userdata('account_id') != null) {  // if there is an account
            $this->load->model('profile'); //model
            $this->load->model('account'); //model
            $data['user'] = $this->profile->get_profile($this->session->userdata('account_id')); //get_profile is a function in model 
            $data['account'] = $this->account->get_account($this->session->userdata('account_id')); //get_account is a function in model
            $data['view'] = 'users/settings';
            $data['title'] = "User's Settings";
            $data['error'] = $error;
            $this->load->view('masterpage', $data);
        } else {
            redirect(base_url('index.php/qablog/login'));
        }

    }
    else
    {
    //if no error
        $data = $this->upload->data();
        $updateProfile = array(
                'profile_pic' => $data['file_name']
            );
        $this->load->model('profile');
        $this->profile->update_profile($this->session->userdata('account_id'), $updateProfile); // update the profile of the user
        redirect(base_url('index.php/users/profile'));
    }
}

模型get_profile():

public function get_profile($profile_id) 
    {
        $this->db->select()->from('profile_tbl')->where('profile_id', $profile_id);
        $query = $this->db->get();

        return $query->first_row('array');
    }

模型update_profile():

public function update_profile($profile_id, $data)
    {
        $this->db->where('profile_id', $profile_id);
        $this->db->update('profile_tbl', $data);
        return $this->db->affected_rows();
    }

模型get_account():

public function get_account($account_id)
    {
        $this->db->select()->from('account_tbl');
        $this->db->where('account_id', $account_id);

        $query = $this->db->get();

        return $query->result_array();
    }

查看:

//if there is an error

                     <?php
                    if ($error == 3) {?>
                       <div class="alert alert-success">
                            <a href="#" class="close" data-dismiss="alert">&times;</a>
                            <strong>Success!</strong> Account or Profile Changed Successfully.
                        </div>
                <?php } else if ($error == 1) { ?>
                       <div class="alert alert-warning">
                            <a href="#" class="close" data-dismiss="alert">&times;</a>
                            <strong>Warning!</strong>Password Entered is Incorrect!.
                        </div>
                <?php  }else if ($error == 2) { ?>
                       <div class="alert alert-warning">
                            <a href="#" class="close" data-dismiss="alert">&times;</a>
                            <strong>Warning!</strong>New Password and Confirm Password!.
                        </div>
                <?php }?>
                // default.png if haven't uploaded profile picture
                <?php $profilePic = "default.png"; ?>
                // if already uploaded profile picture it will display
                <?php if($user['profile_pic'] != null) { ?>
                    <?php $profilePic = $user['profile_pic']; ?>
                <?php }  ?>
                // if there is an error in uploading
                <?php if(isset($upload_error)) { ?>
                    <div class="col-lg-12">
                        <div class="alert alert-danger alert-dismissable">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                            Uploading profile image could not be completed. <?php print_r($upload_error); ?>
                        </div>
                    </div>
                <?php } ?>
                <div class="col-lg-12">
                     //display the profile picture
                    <img src="<?php echo base_url('uploads/profile_pics/'.$profilePic); ?>" width="100" />
    // call the controller upload_profile                    
<?php echo form_open_multipart(base_url('index.php/users/upload_profile'));?>
                        <input type="file" name="userfile" id="userfile" size="20" style="display:none;" />
                        <label for="userfile" class="btn btn-info btn-sm">Choose Image</label>
                        <input type="submit" class="btn btn-xs" value="edit profile" />
                    </form>
                    <h3 class="text-info"><?php echo $user['fname'].' '.$user['lname']; ?></h3>
                </div>  

按照这个,如果你有问题,请告诉我。 :)