如何修复 PHP 代码中的未定义变量?
How to fix undefined variable in PHP code?
这是我在 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
当我点击更新按钮时,它显示错误未定义的变量。 undefined variable 是什么意思?我认为我的错误不是来自未定义的变量。
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>
问题是你的方法需要在调用时给出一个参数,
该参数在您的 Home 控制器方法 ubahdata
中是 $nohp
。因此,如果您想解决此问题,您要么必须在调用该方法时传递该参数,要么删除 it.I 不明白此变量的用途并查看您的 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);
}
这是我在 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
当我点击更新按钮时,它显示错误未定义的变量。 undefined variable 是什么意思?我认为我的错误不是来自未定义的变量。
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>
问题是你的方法需要在调用时给出一个参数,
该参数在您的 Home 控制器方法 ubahdata
中是 $nohp
。因此,如果您想解决此问题,您要么必须在调用该方法时传递该参数,要么删除 it.I 不明白此变量的用途并查看您的 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);
}