繁体   English   中英

无法更新数据并存储在数据库中

[英]unable to update the data and store in database

我已经创建了具有正确验证的注册表单,并且已将其插入数据库中。 现在我正在尝试更新记录,但我不知道为什么我的记录没有得到更新。 请任何人让我知道我的记录未更新可能是什么问题。 请从早上开始尝试,但无法获得理想的结果。 即使验证也不起作用。 请任何人查看我的代码,然后告诉我。

From.php(控制器)

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

   if($this->input->post('update'))
    {
     $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]|is_unique[form.username]');
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[form.email]');

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

            if($this->upload->do_upload('filename'))
            {
              $fn=$this->input->post('fname');
              $ln=$this->input->post('lname');
              $un=$this->input->post('username');
              $em=$this->input->post('email');
              $fi = $this->upload->data('file_name');
              $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
              echo 'Successfully updated your record';
            }
            }
            }
        }

From_model.php

    //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."'");
    }

update_records.php

    <body>
    <?php
      $i=1;
      foreach($data as $row)
      {
      ?>
        <?php echo form_open_multipart(''); ?>

                <div class="container">

                    <h5 style="color:#ff1a1a">First name</h5>
                    <input type="text" class="form-control" name="fname" value="<?php echo $row->first_name; ?>"/>
                    <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 $row->last_name; ?>"/>
                    <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 $row->username; ?>"/>
                    <span class="text-danger"><?php echo form_error("username");?></span>

                    <h5 style="color:#ff1a1a">E-mail</h5>
                    <input type="text" class="form-control" name="email" value="<?php echo $row->email; ?>"/>
                    <span class="text-danger"><?php echo form_error("email");?></span>

                    <h5 style="color:#ff1a1a">Image</h5>
                    <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="">

                    <br><input type="submit" name="update" class="btn btn-success" value="Update Records"/></td>

                </div>

        </form>
        <?php } ?>
    </body>

确保在控制器中加载表单验证库

$this->load->library('form_validation');

同样在模型中,您应该尝试使用以下查询:

$queryString ="update form SET first_name='?, last_name= ?, username=?, email=?, filename=? where ID=?"
$this->db->query($queryString, array($fn,$ln,$un,$em,$fi,$id)); 

如果仍然无法获取任何信息,请尝试var_dump($_POST)来确保您实际上正在获取您的帖子数据。

让我知道是否有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM