如何修复PHP代码中的未定义变量?

时间:2019-03-31 14:10:43

标签: php codeigniter undefined

当我在view / tampil.php中单击更新按钮时,这是我的错误:

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Home::ubahdata()
Filename: controllers/Home.php
Line Number: 46
Backtrace:

File: A:\Sites\PHP_CI\CRUD\application\controllers\Home.php
Line: 46
Function: _error_handler

File: A:\Sites\PHP_CI\CRUD\index.php
Line: 315
Function: require_once
A PHP Error was encountered

Severity: Notice
Message: Undefined variable: nohp
Filename: controllers/Home.php
Line Number: 52

Backtrace:

File: A:\Sites\PHP_CI\CRUD\application\controllers\Home.php
Line: 52
Function: _error_handler

File: A:\Sites\PHP_CI\CRUD\index.php
Line: 315
Function: require_once

当我单击更新按钮时,它显示错误未定义变量。未定义的变量在哪里意味着什么?我认为我的错误不是来自未定义的变量。

models / mahasiswa_model.php:

public function getUser($nohp)
    {
        $query = $this->db->get_where('mahasiswa', array('nohp' => $nohp));
        return $query->row_array();
    }
    public function ubah_model_data($user, $nohp)
    {
        $this->db->where('mahasiswa.nohp', $nohp);
        return $this->db->update('mahasiswa', $user);
    }
    public function ubah_model($nohp)
    {
        $query = $this->db->get_where('mahasiswa', array('nohp' => $nohp));
        return $query->row_array();
    }

这是我来自controller / home.php的代码:

public function ubahdata($nohp)
    {
        $user['nohp'] = $this->input->post('nohp');
        $user['nama'] = $this->input->post('nama');
        $user['alamat'] = $this->input->post('alamat');

        $query = $this->mahasiswa_model->ubah_model_data($user, $nohp);
    }
    public function ubah($nohp)
    {
        $data['mahasiswa'] = $this->mahasiswa_model->ubah_model($nohp);
        $this->load->view('home/tampil', $data);
    }

这是我来自view / tampil.php的代码:

<form method="post" action="<?= base_url('home/ubahdata/'); ?>">
                     <?= $this->session->flashdata('message'); ?>
                     <div class="form-group row">
                         <div class="col-sm mb-3 mb-sm-0">
                             <label for="nohp">Nomer Handphone</label>
                             <input type="text" class="form-control form-control-user" id="nohp" name="nohp" placeholder="Masukkan Nomer Handphone" value="<?php echo $tampil->nohp; ?>">
                         </div>

1 个答案:

答案 0 :(得分:0)

问题是您的方法要求在每次调用时都给出一个参数, 该参数是Home控制器方法$nohp中的ubahdata。因此,如果要解决此问题,则必须在调用该方法时传递该参数或将其删除。我不了解其目的这个变量并查看您的HTML代码,即可通过POST方法传递该变量,因此您无需将该变量作为参数传递,只需删除它即可。这只是我的理解。

public function ubahdata()
    {
        $user['nohp'] = $this->input->post('nohp');
        $user['nama'] = $this->input->post('nama');
        $user['alamat'] = $this->input->post('alamat');

        $query = $this->mahasiswa_model->ubah_model_data($user,  $user['nohp']);
    }
    public function ubah($nohp)
    {
        $data['mahasiswa'] = $this->mahasiswa_model->ubah_model($nohp);
        $this->load->view('home/tampil', $data);
    }