首页 > 解决方案 > 使用 codeigniter 在同一视图页面中编辑和添加

问题描述

我正在使用 codeigniter 创建一个注册表单。

我已经成功完成了插入部分,现在我正在尝试使用相同的视图页面更新我的记录,我曾使用该页面将值插入到数据库中。

但是,我收到错误:

遇到 PHP 错误 严重性:通知

消息:未定义变量:fn

文件名:views/myform.php

行号:18

任何人都可以帮助我解决这个问题吗?

我无法获取表单中的值。

控制器:

    public function updatedata($id)
    {
      //$id=$this->input->get('id');
      $result['data']=$this->Form_model->displayrecordsById($id);

          $this->form_validation->set_rules('fname', 'First name', 'required');
          $this->form_validation->set_rules('lname', 'Last name', 'required');
          $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
          $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

          if ($this->form_validation->run() == FALSE)
          {
              $this->load->view('myform',$result);
          } 

          else 
          {
              $config['upload_path']   = './uploads/';
              $config['allowed_types'] = 'gif|jpg|png';
              $this->load->library('upload', $config);

                  $fn=$this->input->post('fname');
                  $ln=$this->input->post('lname');
                  $un=$this->input->post('username');
                  $em=$this->input->post('email');
                  if($this->upload->do_upload('filename'))
                       {
                           $fi= $this->upload->data('file_name');
                           $file = ("uploads/".$result['data'][0]->filename);
                            //delete the existing file if needed
                            if (file_exists($file)) {
                                unlink($file);
                            }
                       }
                  else
                       {
                          $fi= $result['data'][0]->filename;
                       }
                  $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
                  echo 'Successfully updated your record';
                  //exit();
              } 
    }

模型

    //get values 
        function displayrecordsById($id)
    {
            $query=$this->db->query("select * from form where ID='".$id."'");
            return $query->result();
    }

        //update record
        function updaterecords($fn,$ln,$un,$em,$fi,$id)
    {
            $query=$this->db->query("update form SET first_name='$fn',last_name='$ln',username='$un',email='$em',filename='$fi' where ID='".$id."'");
    }

看法

    <body>

        <?php echo form_open_multipart('form'); ?>
            <div class="container">

                <h2 style="color:#ff1a1a">Registration Form</h2><br>

                <h5 style="color:#ff1a1a">First Name</h5>
                <input type="text" class="form-control" name="fname" value="<?php echo set_value("first_name", $fn); ?>" size="50">
                <span class="text-danger"><?php echo form_error("fname");?></span>

                <h5 style="color:#ff1a1a">Last Name</h5>
                <input type="text" class="form-control" name="lname" value="<?php echo set_value("last_name", $ln); ?>" size="50">
                <span class="text-danger"><?php echo form_error("lname");?></span>

                <h5 style="color:#ff1a1a">Username</h5>
                <input type="text" class="form-control" name="username" value="<?php echo set_value("username", $un); ?>" size="50">
                <span class="text-danger"><?php echo form_error("username");?></span>

                <h5 style="color:#ff1a1a">Email Address</h5>
                <input type="text" class="form-control" name="email" value="<?php echo set_value("email", $em); ?>" size="50">
                <span class="text-danger"><?php echo form_error("email");?></span>

                <h5 style="color:#ff1a1a">Password</h5>
                <input type="password" class="form-control" name="password" value="" size="50">
                <span class="text-danger"><?php echo form_error("password");?></span>

                <h5 style="color:#ff1a1a">Password Confirm</h5>
                <input type="password" class="form-control" name="passconf" value="" size="50">
                <span class="text-danger"><?php echo form_error("passconf");?></span><br>

                <h5 style="color:#ff1a1a">Upload image</h5>
                <input type="file" name="filename" size="20">
                <img src="<?php echo base_url('uploads/' . $row->filename);?>" class="img-responsive" alt="image" width="100px" height="100px"><br><input type="file" name="filename" value="">
                <span class="text-danger"><?php echo form_error("filename");?></span><br>

                <div><input type="submit" value="Submit" class="btn btn-success"></div>

            </div>
        </form>

    </body>

标签: phpcodeigniter

解决方案


在您看来,使用 $fn 之类的:

`$data[0]->$fn`

这是因为 result[' data '] 是您传递给视图的内容。您的数据作为数组来自您的模型,但由于您通过 Id 查询,因此您可以在这种情况下使用 [0]。假设您的 ID 是唯一的。

如果每个 ID 有许多不同的记录,则必须使用 foreach 循环来获取所有记录。

您必须使用 $fn、$ln 等来执行此操作。

您还应该阅读以下内容:https ://www.codeigniter.com/userguide3/database/queries.html

如果要处理用户输入数据,您的查询是不安全的。


推荐阅读