如何修复PHP代码中未定义的变量?
这是我在view/tampil.php中单击更新按钮时的错误:如何修复PHP代码中未定义的变量?,php,codeigniter,undefined,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
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>
问题是您的方法要求在调用时提供一个参数,
在主控制器方法中,该参数是$nohp
。因此,如果要解决此问题,您必须在调用该方法时传递该参数或将其删除。我不理解此变量的用途,查看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);
}