繁体   English   中英

使用 codeigniter4 或 codeigniter3 更新数据库 phpmyadmin

[英]Updating Database phpmyadmin with codeigniter4 or codeigniter3

对不起,我是 CI4 或 CI3 的新手,

我想用表单更新我的数据库。 但是它不能更新我想要的地方,即使我把代码放在里面,如果我删除代码,他们会更新我数据库中的所有记录,请我一直在阅读 CI4 的文档,但我仍然不明白为什么我的代码无法检测到 where 代码。

这是我的完整控制器代码

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

class User extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index()
    {

        $data['title'] = 'My Profile';
        $data['user'] = $this->db->get_where(
            'user',
            ['email' => $this->session->userdata('email')]
        )->row_array();

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar');
        $this->load->view('templates/topbar');
        $this->load->view('user/index');
        $this->load->view('templates/footer');
    }

    public function edit()
    {
        $data['title'] = 'Edit Profile';
        $data['user'] = $this->db->get_where(
            'user',
            ['email' => $this->session->userdata('email')]
        )->row_array();

        $this->form_validation->set_rules('name', 'Full Name', 'required|trim');


        if ($this->form_validation->run() ==  false) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar');
            $this->load->view('templates/topbar');
            $this->load->view('user/edit');
            $this->load->view('templates/footer');
        } else {
            //update data name / bio
            $name = $this->input->post('name');
            $email = $this->input->post('email');
            $bio = $this->input->post('bio');
            $data = array(
                "name" => $name,
                "bio"  => $bio,
            );
            $this->db->where('email', $email);
            $this->db->update('user', $data);

            $this->session->set_flashdata('message', '<div class="alert alert-success" style="text-align:center;" role="alert">
    Your profile has been updated!</div>');
            redirect('user/edit');
        }

    }
}

而且我不使用任何模型:/

下面 TimBrownlaw 的评论回答了我的问题。 我应该在我的数据库表中使用主键,而不是使用电子邮件列。

新代码:

$name = $this->input->post('name');
$id = $this->input->post('id');
$bio = $this->input->post('bio');
$data = array(
                "name" => $name,
                "bio"  => $bio,
            );

$this->db->set($data);
$this->db->where('id', $id);
$this->db->update('user');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM