我无法使用代码点火器更新数据库

时间:2019-07-12 07:59:38

标签: php codeigniter

我正在使用控制器从模型中调用更新功能,但是当我尝试更新列名时,这是行不通的。

----控制器----

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class editprofile extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user');
    }

    public function index()
    {

        $this->form_validation->set_rules('email', 'email', 'required|valid_email');
        $this->form_validation->set_rules('namalengkap', 'namalengkap', 'required');
        $this->form_validation->set_rules('password', 'PASSWORD', 'required');

        if ($this->form_validation->run() == false) {
            $this->load->view('v_profile');
        } else {
            $row = $this->session->userdata('id');
            $data = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'password' => $this->input->post('password'),
                'phone' => $this->input->post('phone'),
                'address' => $this->input->post('address'),
                'kecamatan' => $this->input->post('kecamatan'),
                'kelurahan' => $this->input->post('kelurahan'),
                'rt' => $this->input->post('rt'),
                'rw' => $this->input->post('rw'),
                'zip code' => $this->input->post('zip code'),
                'city' => $this->input->post('city'),
                'province' => $this->input->post('province'),
            );
            $result = $this->user->update($data, $row);
            if ($result > 0) {
                $this->updateProfil();
                $this->session->set_flashdata('msg', show_succ_msg('Data Profile Berhasil diubah, silakan lakukan login ulang!'));
                redirect('c_login/profile');
            } else {
                $this->session->set_flashdata('msg', show_err_msg('Data Profile Gagal diubah'));
                redirect('c_login/profile');
            }
        }
    }
}

-----模型-----

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Model
{

    private $uid = 'id';
    private $tabel = 'users';
    public function __construct()
    {
        parent::__construct();
    }


    public function login($email, $password)
    {
        $this->db->where('email', $email);
        $this->db->where('password', $password);
        return $this->db->get($this->tabel);
    }


    function update($data, $id)
    {

        $this->db->where($this->uid, $id);
        $this->db->update($this->tabel, $data);
    }

    public function get_by_cookie($kue)
    {
        $this->db->where('cookie', $kue);
        return $this->db->get($this->tabel);
    }
}

-----核心/ MY_Controller -----

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user');
    }

    public function updateProfil()
    {
        if ($this->userdata != '') {
            $data = $this->user->select($this->userdata->email);

            $this->session->set_userdata('userdata', $data);
            $this->userdata = $this->session->userdata('userdata');
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在更新功能中,您没有设置任何值。作为 set 方法的必需数组值,您应该提供if。好的做法是在更新之前,先使用设置,然后再更新表。请按照以下代码更新您的 users 表。

function update($data, $id)
{
    $this->db->set($data);
    $this->db->where($this->uid, $id);
    $this->db->update($this->tabel);
}

使用此代码。我希望它能很好地工作。

答案 1 :(得分:0)

在您的控制器中

$result = $this->user->update($data, $row);

变量数据为数组

因此在您的模型中:

function update($data=array(), $id){
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}