首页 > 解决方案 > 如何将数据库中的数据显示到 CodeIgniter 中的输入表单

问题描述

*我想将数据库中的数据显示到输入表单,我试图搜索,但它向我显示了表格我该如何解决这个问题......错误总是显示在 foreach 行,它说未定义的变量和提供的参数无效

谢谢你.....*

该模型

class Profile_model extends CI_Model {

   public function fetch_data(){
      $query = $this->db->get("tbl_admin"); //select * from tbl_admin
      return $query;
   }

}

控制器

class Profile extends CI_Controller {

    public function index()
    {
        $this->load->database();
        $this->load->model('profile_model');
        $data["fetch_data"] = $this->profile_model->fetch_data();
        $this->load->view('bootstrap/dashboard/settings_modal', $data);
    }

}

风景

<?php foreach ($fetch_data as $value) { 
            $username = $value['username'];
            $email = $value['email'];
            $contact = $value['contact'];
            $password = $value['password'];?>

      <?php $attributes = array('id'=>'login_form1', 'class'=>'form'); ?>
      <?php echo form_open('profile', $attributes); ?>
      
        <div class="form-group text-left">
          <label> Username </label>
          <input type="text" class="form-control" value="<?php echo $value['username']; ?>" placeholder="Enter Username" name="username" required>
        </div>

        <div class="form-group text-left">
          <label> Email </label>
          <input type="Email" class="form-control" value="<?php echo $value['email']; ?>" placeholder="Enter Email" name="email" required>
        </div>

        <div class="form-group text-left">
          <label> Contact Number</label>
          <input type="number" class="form-control" value="<?php echo $value['contact']; ?>" placeholder="Enter Contact Number" name="username" required>
        </div>

        <div class="form-group text-left">
          <label> Password </label>
          <input type="Password" class="form-control" value="<?php echo $value['password']; ?>" placeholder="Enter Password" name="password" required>
        </div>

        <div class="form-group text-left">
          <label> Confirm Password </label>
          <input type="Password" class="form-control" value="<?php echo $value['password']; ?>" placeholder="Confirm Password" name="confirm" required>
        </div>
        
        <button class="btn btn-primary float-right" type="button">Submit</button>
      <?php echo form_close(); ?>
      <?php } ?>

标签: phpcodeignitercodeigniter-3

解决方案


$fetch_data 现在只是查询,您必须使用结果或行函数获取数据

<?php
 foreach($fetch_data->result() as $value)
 {
  $username = $value->username;
   //bla bla
   }?>

推荐阅读