Welcome Guest, Not a member yet? Register   Sign In
Cannot update database fields using model->save()
#1

Hi guys,

So I'm working on a CodeIgniter school project and I got stuck while trying to update the user profile. I cannot for the life of me figure out why model->save() does nothing in the db and model->replace works BUT my password isn't hashed before updating the db field with the new password.


This is my controller function called "profile".



PHP Code:
    public function profile()
    {
        
//$data = [];
        
helper(['form']);
        
$model = new UserModel();

        if (
$this->request->getMethod() == 'post') {
            
//Validation here
            
$rules = [
                
'firstname' => 'required|min_length[3]|max_length[20]',
                
'lastname' => 'required|min_length[3]|max_length[20]',
            ];


            if (
$this->request->getPost('password') != '') {
                
$rules['password'] = 'required|min_length[8]|max_length[255]';
                
$rules['password_confirm'] = 'matches[password]';
            }

            if (!
$this->validate($rules)) {
                
$data['validation'] = $this->validator
            } else {
                
                
$newData = [
                    
//'id_user' => session()->get('id_user'),
                     
                    
'firstname' => $this->request->getPost('firstname'), 
                    
'lastname' => $this->request->getPost('lastname'),
                    
//'email' => session()->get('email'),
                
];
                if (
$this->request->getPost('password') != '') {
                    
                    
$newData['password'] = $this->request->getPost('password');
                    
                }
                else 
                {
                    return 
redirect()->to('profile');
                }

                
$id session()->get('id_user');
                
                
$model->update($id,$newData);
            

                
session()->setFlashdata('success''Successfuly Updated');
                return 
redirect()->to('profile');
            }
        }

        
$data['user'] = $model->where('id_user'session()->get('id_user'))
        ->
first();

        echo 
view('templates/header'$data);
        echo 
view('profile'$data);
        echo 
view('templates/footer');
    }

This is my Model

PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
UserModel extends Model
{
    protected $table 'users';
    protected $primarykey 'id_user';

    protected $allowedFields = ['firstname''lastname''email''password''updated_at'];

    protected $beforeInsert = ['beforeInsert'];
    protected $beforeUpdate = ['beforeUpdate'];

    protected function beforeInsert(array $data)
    {
        $data $this->passwordHash($data);

        return $data;
    }

    public function beforeUpdate(array $data)
    {
        $data $this->passwordHash($data);

        return $data;
    }

    public function passwordHash(array $data)
    {

        if (!isset($data['data']['password']))

        $temp = [];
        $temp $data['data']['password'];

        $data['data']['password'] = password_hash($tempPASSWORD_DEFAULT);

        return $data;
    }
   


The view file

Code:
<div class="container">
    <div class="row">
        <div class="col-12 col-sm8- offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white form-wrapper">
            <div class="container">
                <h3><?= $user['firstname'] . ' ' . $user['lastname'] ?></h3>
                <hr>
                <?php if (session()->get('success')) : ?>
                    <div class="alert alert-success" role="alert">
                        <?= session()->get('success') ?>
                    </div>
                <?php endif; ?>
                <form class="" action="profile" method="post">
                    <div class="row">
                        <div class="col-12 cl-sm-6">
                            <div class="form-group">
                                <label for="firstname">Firstname</label>
                                <input type="text" class="form-control" name="firstname" id="firstname" value="<?= set_value('firstname', $user['firstname']) ?>">
                            </div>
                        </div>
                        <div class="col-12 cl-sm-6">
                            <div class="form-group">
                                <label for="lastname">Lastname</label>
                                <input type="text" class="form-control" name="lastname" id="lastname" value="<?= set_value('lastname', $user['lastname']) ?>">
                            </div>
                        </div>

                        <div class="col-12 cl-sm-6">
                            <div class="form-group">
                                <label for="email">E-mail address</label>
                                <input type="text" class="form-control" readonly id="email" value="<?= $user['email'] ?>">
                            </div>
                        </div>
                        <div class="col-12 col-sm-6">
                            <div class="form-group">
                                <label for="password">Password</label>
                                <input type="password" class="form-control" name="password" id="password" value="">
                            </div>
                        </div>
                        <div class="col-12 col-sm-6">
                            <div class="form-group">
                                <label for="password_confirm">Confirm Password</label>
                                <input type="password" class="form-control" name="password_confirm" id="password_confirm" value="">
                            </div>
                        </div>

                        <?php if (isset($validation)) : ?>
                            <div class="col-12">
                                <div class="alert alert-danger" role="alert">
                                    <?= $validation->listErrors() ?>

                                </div>
                            </div>
                        <?php else :   ?>
                        <?php endif; ?>

                    </div>

                    <div class="row">
                        <div class="col-12 col-sm-2">
                            <button type="submit" class="btn btn-primary">Update</button>
                        </div>

                    </div>
                </form>

            </div>
        </div>
    </div>
</div>

Any help is greatly apreciated.
Reply
#2

Did you try using set on the data array?

PHP Code:
$model->set($newData);
$model->update($id,$newData); 

If that wont work then you have problems else where in your code.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

The problem was a typo in my Model. I wrote primarykey instead of primaryKey...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB