簡體   English   中英

更改用戶數據庫信息在CodeIgniter中不起作用

[英]Change user database information not working in CodeIgniter

我正在嘗試創建一個函數,以便用戶可以更改數據庫中的電子郵件。 提交表單時出現錯誤,並且嘗試提交表單時未更改登錄用戶的電子郵件。

我的控制器:

<?php
class GwController extends CI_Controller {
    // index functie - hierin laad ik de view file 'gegevenswijigen' in het mapje views.
    public function index()
    {
        $this->load->view('gegevenswijzigen');
    }

    function __construct()
    {
         parent::__construct();
         //Laad het Update_model in models folder
         $this->load->model('Update_model');
    }

    //Controller functie om email van een gebruiker te veranderen
     function update_email() 
     {
         $id= $this->input->post('user_id');
         $data = array(
         'email' => $this->input->post('email'));

         $wachtwoord = $_POST['wachtwoord'];

         //check gebruiker in database
         $this->db->select('*');
         $this->db->from('users');
         $this->db->where(array('user_id'=>$_SESSION['user_id'], 'wachtwoord' => $wachtwoord));
         $query = $this->db->get();

         $user = $query->row();
         //Als gebruiker bestaat
        if ($user->user_id) {
            $this->Update_model->update_email($id, $data);
            header ('location:https://kadokado-ferran10.c9users.io/user/profile');
         }
     }
}

我的模型功能:

<?php
class Update_model extends CI_Model{


// Model update functie om email te veranderen
function update_email($id,$data){
$this->db->where('user_id', $id);
$this->db->update('users', $data);
$_SESSION['email'] = $data['email'];
//header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}

}

我的觀點:

 <form action="<?php echo base_url() . "GwController/update_email"?>" method="post">
                <tr>
                    <td><h4>E-mail adres wijzigen</h4></td>
                    <td><?php echo form_input(array('type'=>'email','id'=>'email', 'name'=>'email', 'placeholder' => 'Nieuw e-mail adres:', 'size'=>70));?></td>
                    <td><?php echo form_input(array('type'=>'password','id'=>'wachtwoord', 'name'=>'wachtwoord', 'placeholder' => 'Uw wachtwoord:', 'size'=>70));?></td>
                    <td><?php echo form_input(array('type'=>'hidden','id'=>'user_id', 'name'=>'user_id', 'value' => $_SESSION['user_id'], 'size'=>70));?></td>
                    <td><button type="submit" name="emailwijzigen" class="btn btn-primary">Opslaan</button></td>
                </tr>
            </form>

這是我嘗試填寫表格並提交時收到的錯誤:

A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/GwController.php

Line Number: 45

Backtrace:

File: /home/ubuntu/workspace/application/controllers/GwController.php
Line: 45
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

這是第45行:

if ($user->user_id) {

任何幫助表示贊賞,謝謝

讓我們看一下故障排除步驟。

  1. 是否設置了$ _SESSION ['user_id']? echo 'sessid:'.print_r($_SESSION['user_id']); 在功能啟動時進行檢查。
  2. 查詢會產生任何結果嗎? echo 'any results?:' . $query->num_rows(); $user = $query->row();
  3. 如果沒有,那么讓我們使用echo $this->db->get_compiled_select();手動檢查echo $this->db->get_compiled_select(); 而不是$query = $this->db->get(); 並手動嘗試使用phpmyadmin中生成的查詢。
  4. 如果phpmyadmin不返回任何行,並且num_rows()返回任何行,則條件$this->db->where(array('user_id'=>$_SESSION['user_id'], 'wachtwoord' => $wachtwoord)); 正在以這樣的方式進行評估:具有user_id和密碼的用戶在數據庫中不存在。
  5. 檢查密碼字段是否得到任何echo $_POST['wachtwoord'];

還請考慮這樣的錯誤情況。 您的代碼應始終考慮到錯誤。 我已經自由注釋了您的函數,以便它可以正確處理錯誤。

function update_email() {
    $id = $this->input->post('user_id');
    $email = $this->input->post('email');
    $wachtwoord = $this->input->post('wachtwoord');

    // input->post returns null if not set, we need all these variables
    // thus they should all be set
    if (is_null($id) || is_null($email) || is_null($wachtwoord)) {
        exit('Missing parameters'); // handle this nicer with session flash messages
    }

    //check gebruiker in database
    $this->db->select('*');
    $this->db->from('users');
    $this->db->where(array('user_id' => $_SESSION['user_id'], 'wachtwoord' => $wachtwoord));
    $query = $this->db->get();

    // there should only be one valid user for the given
    // user_id and password... otherwise we have no results and $user->user_id
    // will fail or we have a bigger problem on our hands (multiple users w/
    // the same id
    if ($query->num_rows() !== 1) {
        exit('User error');
    }

    $user = $query->row();
    //Als gebruiker bestaat
    // not sure what the point of this conditional statement is...
    // could move inside num_rows()
    if ($user->user_id) {
        $this->Update_model->update_email($id, array('email' => $email));
        header('location:https://kadokado-ferran10.c9users.io/user/profile');
    } else {
        // do something (should always have an else statement in a case like this)
    }
}

進一步請閱讀以下內容: https : //codeinphp.github.io/post/controllers-for-frontend-and-backend-in-codeigniter/

這是處理Admin/LoggedInPublic部分的更好的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM