在php codeigniter

时间:2015-12-29 09:49:38

标签: php database html5 image codeigniter

我正在尝试使用php codeigniter从表单验证方法向数据库插入图像和数据。这是我的代码,问题是不会将数据和图像插入数据库。请帮忙。 控制器页面

public function gallery5()
{  
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('upload');
$this->load->model('Login_set');
$page_id =$this->uri->segment(3);   
$data['e']=$this->Login_set->select1(); 
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data);
}  
public function gallery5_insert()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('events','events','required');
$this->form_validation->set_rules('description','description','required');
if ($this->form_validation->run() == true)
{       
$config['upload_path']          = 'application/views/App_stay/photo1/';
$config['max_size']             = '100';
$config['max_width']            = '1024';
$config['max_height']           = '768';
$this->load->library('upload', $config);
$field_name = "images";
if ( ! $this->upload->do_upload($field_name))
{
$error = array('error' => $this->upload->display_errors());
$page_id =$this->uri->segment(3);
$data['e']=$this->Login_set->select1_edit();       
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data, $error);
}
else 
{
$image_path = $this->upload->data();
$this->Login_set->add_events();
$page_id =$this->uri->segment(3);
$data['e']=$this->Login_set->select1_edit();      
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data);
}
} 
} 

查看页面

<form action="<?php echo base_url();?>index.php/Welcome/gallery5_insert" method="post" enctype="multipart/form-data">
<?php
foreach ($e->result_array() as $value){       
?>     
<div class="form-group has-info ">
<input type="hidden" name="id" value="<?php echo $value['event_id'];?>">
<?php }?>              
<div class="form-group has-info col-lg-7">
<label class="control-label" for="inputSuccess">Event title</label>
<input type="text" class="form-control" name="events" id="events" >
</div>                                   
<div class="form-group has-info col-lg-7 ">
<label>Event Description</label>
<textarea id="description" name="description" class="form-control " rows="3"></textarea><br><br>
</div>
<div class="form-group has-info col-lg-7">
<label>Event Related images</label>
<input type="file" name="images"><br>
</div>
<div class="form-group has-info col-lg-7">
<button type="submit" class="btn btn-primary">
<span>SUBMIT</span>
</button>
</div>
</div>     
</form> 

模型页面

public function add_events()
{ 
$this->form_validation->set_message('events','events','required');
$this->form_validation->set_message('description','description','required');
$this->form_validation->set_message('images','images','required');
$this->load->database();
$this->load->helper('url');
$data = array(
'event_id'=>'',
'hotel_id'=>1,
'event_title'=>$this->input->post('events'),
'event_description'=>$this->input->post('description'),
'image_path'   =>    $image_path[full_path]                       
);
$this->db->insert('event_hotel1', $data);
}

2 个答案:

答案 0 :(得分:1)

您需要将$image_path传递给模型文件

所以改变

$this->Login_set->add_events();

$this->Login_set->add_events($image_path);// pass your argument

在MODEL FILE中

public function add_events($image_path){......

答案 1 :(得分:0)

您可以在这里修复一些事项,让您的生活更容易管理。首先,添加__construct()函数来加载公共库,帮助程序和模型将为您节省大量重复代码。如果您不熟悉,__construct()对象中的Class函数将在每次初始化该类时运行,因此在那里加载资源很常见等。

请注意,.php功能也已从$this->load->view()功能中删除,因为没有必要。

我们还使用[full_path]中的do_upload()属性向您模型中的add_events()函数传递图像路径。

您的控制器

/**
* Use a __construct function so you don't have to repeat things
*/
public function __construct() {
    // Helpers & Libraries
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');

    // Models
    $this->load->model('Login_set');

    // Upload Library & Config
    $config['upload_path'] = 'application/views/App_stay/photo1/';
    $config['max_size']    = '100';
    $config['max_width']   = '1024';
    $config['max_height']  = '768';
    $this->load->library('upload', $config);
}

// Get the Gallery
public function gallery5()
{  
    $page_id =$this->uri->segment(3);   
    $data['e']=$this->Login_set->select1(); 
    $this->load->view('App_stay/pages/hotel1_galery_event',$data);
}

public function gallery5_insert()
{
    $this->form_validation->set_rules('id','id');
    $this->form_validation->set_rules('events','events','required');
    $this->form_validation->set_rules('description','description','required');

    if ($this->form_validation->run() == true)
    {   
        $field_name = "images";

        if ( ! $this->upload->do_upload($field_name))
        {
            $error = array('error' => $this->upload->display_errors());
            $page_id =$this->uri->segment(3);
            $data['e']=$this->Login_set->select1_edit();       
            $this->load->view('App_stay/pages/hotel1_galery_event',$data, $error);
        }
        else 
        {
            $image_upload = $this->upload->data();
            $this->Login_set->add_events($image_upload['full_path']);
            $page_id =$this->uri->segment(3);
            $data['e']=$this->Login_set->select1_edit();      
            $this->load->view('App_stay/pages/hotel1_galery_event',$data);
        }
    } 
} 

您的模型

/**
* Add a __construct()
*/
public function __construct() {
    $this->load->database();
    $this->load->helper('url');
}

public function add_events( $image_path = '' )
{ 
    // NOTE: you don't seem to be doing anything with this
    // Also, these should be set_rules, not set_message
    $this->form_validation->set_rules('events','events','required');
    $this->form_validation->set_rules('description','description','required');
    $this->form_validation->set_rules('images','images','required');

    $data = array(
        'event_id'=>'',
        'hotel_id'=>1,
        'event_title'=>$this->input->post('events'),
        'event_description'=>$this->input->post('description'),
        'image_path'   =>    $image_path                      
    );
    $this->db->insert('event_hotel1', $data);
}

有关使用set_rules,set_message和CI form_validation库的一些其他信息,请查看:https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html