使用codeigniter 3将数据插入数据库

时间:2015-11-30 22:48:07

标签: php mysql codeigniter

我不确定我的问题措辞是否正确,所以请随时告诉我改变它。

我允许一次上传多个图像,然后将文本作为html img插入数据库。

这是我的代码:

if ( ! $this->upload->do_upload('post_image'))
                {
                    $this->session->set_flashdata('post_message', $this->upload->display_errors());
                    $this->session->set_flashdata('post_message_class', 'alert-danger');
                    redirect('/user/profile/'.$identity, 'refresh');
                }
                else
                {
                    $uploaded = $this->upload->data();                          
                    // insert pos
                    if(isset($uploaded['file_name']) && $uploaded['file_name'] == 0){                       
                        $post_text = '<img src="/uploads/images/'.$uploaded['file_name'].'" width="251px" alt="'.$uploaded['raw_name'].'" /><br>'.$this->input->post('post_text');                                  
                    }
                    else 
                    {
                        foreach ($uploaded as $images){                                             
                            $post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');                                                  
                        }   
                    }               

                    $query = $this->user_model->insert_user_posts($this->input->post('poster_id'), $this->input->post('profile_id'), $this->input->post('post_type'), $post_text);
                    $this->session->set_flashdata('post_message', 'Image has been posted!');
                    $this->session->set_flashdata('post_message_class', 'alert-success');
                    redirect('/user/profile/'.$identity, 'refresh');          
                }

编辑:以下是该模型的代码:

public function insert_user_posts($poster_id, $profile_id, $post_type, $post_text)
    {
        // Users table.
        $data = array(
            'poster_id'     => $poster_id,
            'profile_id'    => $profile_id,
            'post_type'     => $post_type,
            'post_text'     => $post_text,
            'datetime'      => time()
        );

        $this->db->insert($this->tables['users_posts'], $data);

        if($this->db->affected_rows() > 0)
        {
            $this->set_message('upload_successful');
            return TRUE;
        }
        else
        {
            return FALSE;   
        }
    }

另外,我使用的是:https://github.com/avenirer/MY_Upload

如果我echovar_dump $post_text它将显示两个图像的数据,但它只插入第一个图像。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

如果要将图像附加在同一$post_text变量中,则必须更改此行: $post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');

将赋值运算符=更改为连接运算符.=,如下所示:

$post_text .= ...

每次您将每个图像添加到其末尾时,而不是覆盖$post_text变量。我可以猜测你的最终foreach循环可能看起来像这样:

$post_text = ""; foreach ($uploaded as $images){ $post_text .= '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" />'; } $post_text .= '<br>'.$this->input->post('post_text');

这将创建一个字符串,其中包含一行中的所有图像,最后是'<br>'.$this->input->post('post_text');。请注意,您必须首先将$post_text变量实例化为某些内容(此处我将其设置为空字符串)才能使用连接运算符。