【问题标题】:how to edit and insert data from the single form如何从单个表单编辑和插入数据
【发布时间】:2013-03-25 08:44:31
【问题描述】:

这是我的控制器。这里的函数add_post() 是用于添加和更新数据的函数。问题是,如果我想编辑表单,那么我可以将数据库中的值获取到相应区域的表单中,但是当我想使用相同的功能添加数据时,我不能。我搜索了很多,但找不到解决方案。

    <?php
    class Blog_controller extends CI_Controller {

        public function index() {
            $this->get_post();
        }

        public function __construct() {
            parent::__construct();
            $this->load->helper('form');
            $this->load->helper('url'); 
            $this->load->model('blog_model');
        }

        function add_post($id) {
            if($id==NULL) {
                return $this->load->view('admin/add_post');
            } else {
                $data['query']=$this->blog_model->get_single_post($id);
                $this->load->view('admin/add_post',$data);
            }
        }

        function do_upload() {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('premium/main', $error);
            } else {
                $this->upload->do_upload();
                $data=$this->upload->data();
                $filename=$data['file_name'];
                $this->blog_model->insert_post($filename);
            }
        }

        function get_post() {
            $data['query']=$this->blog_model->get_post();
            $this->load->view('premium/main',$data);
        }

        function view_table() {
            $data['query']=$this->blog_model->get_post(); $this->load->view('admin/view_table',$data);
        }   
    }
    ?>



MODEL

这是我项目的模型。我想使用单个函数插入和更新数据,即 insert_post()。

    <?php 
Class Blog_model extends CI_Model
{

function __construct()
{
parent::__construct();
$this->load->database();
}




 function insert_post($filename)
 {
 $data=array(
 'title'=>$this->input->post('title'),
'description'=>$this->input->post('description'),
'publish'=>$this->input->post('publish'),
'image'=>$filename,
);

$this->db->insert('post',$data);
 redirect('blog_controller');

}


function get_post()
{
$query = $this->db->get_where('post');
return $query->result();
}
function get_single_post($id)
{
$query = $this->db->get_where('post',array('id'=>$id));
return $query->row($id);
}
}
?> 

这是我的看法。在这种形式中,我想在用户想要插入数据并且用户想要编辑时添加数据,那么数据应该在表单的相应字段中检索并应该更新......

    <html>
    <body>
    <h1>Add Blog Title</h1>
    <?php function add_post($id);?>
    <?php echo form_open_multipart('blog_controller/do_upload');?>
    <p>Title : <input type="text" name="title" value="<?php if($id==0){echo $query->title;}?>"></br></p>
    <p>Description : <textarea name="description" rows="5"> <?php echo $query->title;?></textarea></br/></p>
    <p>Current Image : <img src="<?php echo base_url();?>/uploads/<?php echo $query->image;?>"</b></p>
    <p>Image : <?php echo form_upload('userfile');?></b></p>
    <p>Publish ?: <input type="checkbox" name="publish" value="1" <?php if($query->publish == 1) echo 'checked="checked"';?>></br></p>
    <input type="submit" value="submit"> 

    </form>
    </body>
    </html>

这是我的表格,从中传递了相应的编辑字段ID,因此我们可以查看表格中的数据,并且可以知道我们要编辑哪个数据的哪个字段..

<html>
<body>

<table border="2px">

<tr>
<td>ID</td>
<td>Title</td>
<td>Description</td>
<td>image</td>
<td>Action</td>
</tr>
<?php foreach($query as $a):?>
<tr>
<td><?php echo $a->id;?></td>
<td><?php echo $a->title;?></td>
<td><?php echo $a->description;?></td>
<td><img src="<?php echo base_url();?>/uploads/<?php echo $a->image?>" height="100px" width="100px"></td>
<td><?php echo anchor('blog_controller/add_post/'.$a->id,"Edit");?></td>
<td><?php echo anchor('blog_controler/delete_post/'.$a->id,"Delete");?></td>

<?php endforeach;?>
</tr>




</table>
</body>
</html>

【问题讨论】:

  • 你为什么在这里使用returnreturn $this-&gt;load-&gt;view('admin/add_post');
  • 您想在控制器中创建编辑方法以获取已创建帖子的数据并将这些值“重新加载”到管理员中的“编辑”表单中吗?
  • 我认为有两种可能的选择:1.您的 $id 始终为空 2.您使用 $id 从数据库获取的数据为空。你能满足这两个选项吗?

标签: php codeigniter


【解决方案1】:

您需要验证发送到控制器的表单数据,然后将这些值发送回视图:这是一个示例:

function add_post() 
{
   if ($this->input->post())
   {
      $this->form_validation->set_rules('text', 'Text', 'trim|required|min_length[8]');
   }

   if ($this->form_validation->run() == FALSE)
   {
      // VALIDATION ERROR
   }
   else 
   {
      // SUCCESS
      // code to save title in db ...
      // and redirect to success page
      redirect('admin/posts');
   ]

   $this->load->view('admin/add_post', $data);


}

因此,如果在文本字段中没有发送任何内容并且最小长度不是至少 8 个字符,它将重新加载视图。

如果没问题,这部分的代码会被执行(你需要自己的保存到数据库的功能,现在我没有在那里添加它)并重定向到管理员/帖子。

并以您的形式代替:

<input type="text" name="text" value=""/>

你需要做一些事情,例如:

<input type="text" name="username" value="<?php echo set_value('text'); ?>"/>

【讨论】:

  • 我想你不明白我的问题。我只想从单一表单添加和更新数据库的数据,即控制器的一个功能可以一次完成这些工作......
  • 把你的模型放在你的问题中,这样每个人都能看到。
  • 伙计,一半的东西应该在控制器中,尤其是对于 insert 。只在你的模型中加入$this-&gt;db-&gt;insert('post',$data);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 2019-02-26
  • 2015-11-26
  • 2016-06-17
  • 2019-11-21
  • 1970-01-01
  • 2020-02-21
相关资源
最近更新 更多