gpt4 book ai didi

php - undefined variable 代码点火器

转载 作者:行者123 更新时间:2023-12-02 04:55:40 24 4
gpt4 key购买 nike

我的 View 有这个错误,我找不到问题出在哪里

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: errors

Filename: views/register_user.php

Line Number: 5

查看:

<?php if($errors){ ?>
<div style="background:red,color:white;">
<?=$errors?>
</div>
<? } ?>

这里是 Controller

function register(){
if($_POST){
$config=array(
array(
'field'=>'username',
'label'=>'Username',
'rules'=>'trim|required|min_length[3]|is_unique[users.username]'
),
array(
'field'=>'password',
'label'=>'Password',
'rules'=>'trim|required|min_length[5]|max_length[15]'
),
array(
'field'=>'password2',
'label'=>'Password Confirmed',
'rules'=>'trim|required|min_length[5]|matches[password]'
),
array(
'field'=>'user_type',
'label'=>'User Type',
'rules'=>'required'
),
array(
'field'=>'email',
'label'=>'Email',
'rules'=>'trim|required|is_unique[users.email]|valid_email'
)
);
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
if($this->form_validation->run() == FALSE){
$data['errors']=validation_errors();
} else {
$data=array(
'username'=>$_POST['username'],
'password'=>$_POST['password'],
'user_type'=>$_POST['user_type']
);
$this->load->model('user');
$userid=$this->user->create_user($data);
$this->session->set_userdata('userID',$userid);
$this->session->set_userdata('user_type',$_POST['user_type']);
redirect(base_url().'posts');
}
}
$this->load->helper('form');
$this->load->view('header');
$this->load->view('register_user');
$this->load->view('footer');
}

我开始使用 Codeigniter,但找不到问题...

最佳答案

问题是在某些情况下$errors未创建变量。使用 if(isset($errors) && $errors))

实际上在 View 中您只需要 <?php echo validation_errors(); ?> , 无需将其分配给变量

看看http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html有关如何显示错误的更多信息

关于php - undefined variable 代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18042383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com