gpt4 book ai didi

php - 无法访问 codeigniter 中的空属性

转载 作者:行者123 更新时间:2023-11-30 22:54:37 28 4
gpt4 key购买 nike

我无法从在数据库中插入空值的表单中获取发布值

用户 Controller

public function edit($id = NULL) {
if ($id) {
$this->data ['user'] = $this->user_login_model->get($id);
count ( $this->data ['user'] ) or $this->data ['errors'] [] = "user could not be found";
} else {
$this->data ['user'] = $this->user_login_model->new_user ();
}

$rules = $this->user_login_model->rules_admin;
$id || $rules ['password'] .= '|required';
$this->form_validation->set_rules ( $rules );
if ($this->form_validation->run () == TRUE) {

$data=$this->user_login_model->array_from_post(array('name','email','password'));
$this->data['password']=$this->user_login_model->$data['password'];
$this->user_login_model->save($data,$id);
redirect('admin/users');

// do stuff

}

如果是新用户,那么它将发布新数据

public function new_user() {
$user=new stdClass();
$user->name ='';
$user->email ='';
$user->password ='';
return $user;

var_dump($user);

循环遍历数组并返回它的模型方法

public function array_from_post($fields) {
$data = array ();
foreach ( $fields as $field ) {
$data['feild']=$this->input->post($field);
return $data;
echo var_dump($data);
}
}

这是 var_dump

object(stdClass)[22]
public 'id' => string '18' (length=2)
public 'email' => string '' (length=0)
public 'password' => string '' (length=0)
public 'name' => string '12' (length=2)

这是空的,你能告诉我有什么问题吗?

最佳答案

试试这个
你的 Controller 编辑功能

  public function edit($id = NULL) {
if ($id)
{
$this->data ['user'] = $this->user_login_model->get($id);
count ( $this->data ['user'] ) or $this->data ['errors'] [] = "user could not be found";
}
else
{
$this->data ['user'] = $this->user_login_model->new_user ();
}

$rules = $this->user_login_model->rules_admin;
$id || $rules ['password'] .= '|required';
$this->form_validation->set_rules ( $rules );
if ($this->form_validation->run () == TRUE)
{

$this->data=$this->user_login_model->array_from_post(array('name','email','password'));
$this->data=$this->array_from_post(array('name','email','password'));

// I removed this line not sure what you did here
$this->user_login_model->save($this->data,$id);
// do stuff
redirect('admin/users');

}
}

你的模型 array_from_post 函数

public function array_from_post($fields)
{
$data = array ();
foreach ( $fields as $field )
{
$data[$field]=$this->input->post($field);

}
return $data;

}

你的新用户函数

 public function new_user() {
$user=new stdClass();
$user->name ='';
$user->email ='';
$user->password ='';
return $user;
}

希望对你有帮助

关于php - 无法访问 codeigniter 中的空属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974750/

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