【问题标题】:Codeigniter form populate with db results not workingCodeigniter 表单填充数据库结果不起作用
【发布时间】:2015-03-14 09:38:33
【问题描述】:

我这几天一直在尝试使用表单类从我的数据库中填充文本字段。我也一直在寻找如何在没有任何运气的情况下做到这一点。

请有人看看我的代码并告诉我我做错了什么。

型号

    //This function brings up the selected users information for editing.
public function edit_user($id)
{
    $this->db->select('id, email, name, lastname, homeaddress, posteladdress, 
    mobile, hometel, idnum');
    $this->db->where('id', $id)->limit(1);
    $query = $this->db->get('user');
    return $query->result();

}

控制器

public function get_user_edit($id)
{
    $this->load->helper('form');
    $this->load->model('model_users'); //Load the user model

    //Get the database results from the model

    $data['results'] = $this->model_users->edit_user($id);
    foreach ($data['results'] as $key => $row)
    {
        $data['results'] = array(   'id' => $row->id,
                        'email' => $row->email,
                        'name' => $row->name, 
                        'lastname' => $row->lastname,
                        'homeaddress' => $row->homeaddress,
                        'posteladdress' => $row->posteladdress,
                        'mobile' => $row->mobile,
                        'hometel' => $row->hometel,
                        'idnum' => $row->idnum);

    }
    $this->load->view('edit_user', $data);
}

查看

<div id="body">
        <p>Edit user information.</p>

    <?php 


    echo form_open('user_admin/user_update', $results);

    echo validation_errors();

    echo "<p><lable>Email:</lable>";
    echo form_input('email', set_value('email'));
    echo "</p>";


    echo "<p><lable>Name:</lable>";
    echo form_input('name', set_value('name'));
    echo "</p>";

    echo "<p>";
    echo form_submit('edit_submit', 'Update');
    echo "</p>";

    echo form_close();

    ?> 

我不断收到此错误

遇到 PHP 错误

严重性:4096

消息:stdClass 类的对象无法转换为字符串

文件名:helpers/form_helper.php

行号:1010

【问题讨论】:

    标签: database forms codeigniter populate setvalue


    【解决方案1】:

    第一件事。您期望从数据库中返回一行,对吗?但是你在你的控制器中循环遍历结果。而不是在你的模型中这样做:

    return $query->result();
    

    这样做:

    return $query->row();
    

    并从控制器中删除 foreach 循环(其中甚至还有一个未使用的 $key 变量)。您最终将拥有更简洁、更短的代码。仅当您获得单行时,似乎就是这种情况。

    继续前进。

    您是否阅读过这里的文档:https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

    form_open('user_admin/user_update', $results); - 你想在这里做什么?

    您使用第二个参数将属性传递给开始标记。现在查看您的代码,您的 $results 数组具有每个字段的值。将所有这些都推送到表单开始标记中没有多大意义,是吗?

    使用正确值渲染字段。

    这是文档中如何配置和呈现输入字段的示例:

    $data = array(
                  'name'        => 'username',
                  'id'          => 'username',
                  'value'       => 'johndoe',
                  'maxlength'   => '100',
                  'size'        => '50',
                  'style'       => 'width:50%',
                );
    
    echo form_input($data);
    

    因此,您的电子邮件输入应采用以下最小配置的格式:

    $data = array(
                  'name'        => 'email',
                  'value'       => $results['email']
                );
    
    echo form_input('email', $data);
    

    在验证时(在 user_admin 控制器中的 user_update() 方法中),您必须执行一些逻辑来捕获您正在验证的值。所以你需要这样的东西:

    $results['email'] = set_value('email');
    

    希望这是有道理的!

    【讨论】:

      【解决方案2】:

      非常感谢您的帮助。我相信您一定已经意识到我是 Codeigniter 的新手,但在您的帮助下,我能够解决我遇到的问题。

      我将查询更改为仅返回它发现的一个结果,如您建议的那样

      return $query->row();
      

      你说得对,我不需要 foreach 循环来填充数组以传递给视图。

      感谢您指出我试图将 db 的结果作为表单的属性传递。:)

      我为遇到我的问题的任何人添加了我的代码。也许对他们有帮助。

      我的控制器

          public function get_user_edit($id)
      {
          $this->load->model('model_users'); //Load the user model
      
          //Get the database results from the model
      
          $data['results'] = $this->model_users->edit_user($id);
          $this->load->view('edit_user', $data);
      }
      

      我的模型

          //This fetches the selected users information for editing.
      public function edit_user($id)
      {
          $this->db->select('id, email, name, lastname, homeaddress, posteladdress, 
          mobile, hometel, idnum');
          $this->db->where('id', $id)->limit(1);
          $query = $this->db->get('user');
          return $query->row();
      
      }
      

      我的看法

      <div id="container">
      <h1>Edit user</h1>
      
      <div id="body">
              <p>Edit user information.</p>
      
          <?php 
      
      
      
          echo form_open('user_admin/user_update');
      
          echo validation_errors();
      
          echo form_hidden('id', $results->id);
      
          echo "<p><lable>Email:</lable>";
          echo form_input('email', $results->email);
          echo "</p>";
      
      
          echo "<p><lable>Name:</lable>";
          echo form_input('name', $results->name);
          echo "</p>";
      
          echo "<p><lable>Last name:</lable>";
          echo form_input('lastname', $results->lastname);
          echo "</p>";
      
          echo "<p><lable>Home address:</lable>";
          echo form_input('homeaddress', $results->homeaddress);
          echo "</p>";
      
          echo "<p><lable>Postal address:</lable>";
          echo form_input('posteladdress', $results->posteladdress);
          echo "</p>";
      
          echo "<p><lable>Mobile number:</lable>";
          echo form_input('mobile', $results->mobile);
          echo "</p>";
      
          echo "<p><lable>Home telephone:</lable>";
          echo form_input('hometel', $results->hometel);
          echo "</p>";
      
          echo "<p><lable>ID number:</lable>";
          echo form_input('idnum', $results->idnum);
          echo "</p>";
      
          echo "<p>";
          echo form_submit('edit_submit', 'Update');
          echo "</p>";
      
          echo form_close();
      
          ?> 
      
          </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-27
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 1970-01-01
        • 2012-03-04
        相关资源
        最近更新 更多