codeigniter 3

时间:2017-11-22 13:29:45

标签: php codeigniter

嘿我正在使用此代码运行我的数据表单以在我的数据库中提交数据

当我选择任何图片时它会被保存,但如果我不这样做,那么它就会变成空白而且没有任何内容被提交。

以下是我的代码

查看

<form role="form" action="<?php echo base_url();?>admin/main_image_upload" method="post" enctype="multipart/form-data">

                                    <fieldset class="form-group">

                                        <input type="hidden" name="txt_hidden" value="" class="form-control"> 
                                    </fieldset>

                                    <fieldset class="form-group">
                                        <label class="control-label" for="formGroupExampleInput">Add Main Caregory</label>
                                        <input type="text" value="<?php echo set_value('p_name'); ?>" placeholder="Main Category" name="p_name" class="form-control"> 
                                    </fieldset>
                                    <fieldset class="form-group">
                                        <label class="control-label"  for="formGroupExampleInput2">Order</label>
                                        <input type="text" name="order_id" placeholder="Order Id" value="<?php echo set_value('order_id'); ?>" class="form-control" id="formGroupExampleInput2"> 
                                    </fieldset>

                                    <fieldset class="form-group">
                                        <label class="control-label"  for="formGroupExampleInput2">Status ( 0 active , 1 inactive)</label>
                                        <input type="text" name="status" placeholder="Status" value="<?php echo set_value('status'); ?>" class="form-control" id="formGroupExampleInput2"> 
                                    </fieldset>

                                        <fieldset class="form-group">
                                        <label class="control-label"  for="formGroupExampleInput2">Image</label>
                                        <input type="file" name="p_image" placeholder="Image" value="<?php echo set_value('image'); ?>" class="form-control"> </fieldset>



                                        <div class="form-group">
                                        <button type="submit" class="btn btn-primary">Add Category</button>
                                    </div>
                                </form>

控制器

function main_upload()
{
  $type=explode('.',$_FILES['p_image']['name']);
  $type = $type[count($type)-1];
  $url = "uploads/main_cat/".uniqid(rand()).'.'.$type;
  if (in_array($type,array("jpg","jpeg","png","gif")))
  {
    if(is_uploaded_file($_FILES["p_image"]["tmp_name"]))
      if(move_uploaded_file($_FILES['p_image']['tmp_name'],$url))
      return $url; // returns the url of uploaded image.
  }
  else
  {
    redirect(base_url() . 'admin/main_cat', 'refresh');// redirect to show file type not support message
  }
}



    function main_image_upload()
{
  $data ['main_image']= $this->main_upload();
  $data ['p_name']= $this->input->post('p_name');
  $data ['order_id']= $this->input->post('order_id');
  $data ['status']= $this->input->post('status');
  $this->db->insert('main_category', $data);
  redirect(base_url() . 'admin/main_cat', 'refresh');// Redirect to Success page
}

如果我没有选择任何图像,那么它的运行空白,没有任何内容保存在数据库中。

如何验证空逻辑

3 个答案:

答案 0 :(得分:0)

设置if条件以检查用户是否在您main_upload()功能

中选择了图像
function main_upload(){
    if(!empty($_FILES['p_image']['name'])){
      $type=explode('.',$_FILES['p_image']['name']);
      $type = $type[count($type)-1];
      $url = "uploads/main_cat/".uniqid(rand()).'.'.$type;
      if (in_array($type,array("jpg","jpeg","png","gif"))){
        if(is_uploaded_file($_FILES["p_image"]["tmp_name"]))
          if(move_uploaded_file($_FILES['p_image']['tmp_name'],$url))
          return $url; // returns the url of uploaded image.
      }
      else
      {
        redirect(base_url() . 'admin/main_cat', 'refresh');// redirect to show file type not support message
      }
  }else{
        return "";
  }
}

答案 1 :(得分:0)

您可以添加该条件:

if(!empty($_FILES['p_image']['name'])){

//Upload code

 );

}else{
//Without upload code and database field name value blank

}

答案 2 :(得分:0)

我真的认为您应该使用form_validation和CI upload库。我的意思是,这是你选择框架的原因之一!

好的,所以你的视图必须改变一点,以适应会话错误和我们即将创建的新功能:

<form role="form" action="<?php echo base_url(); ?>admin/insert" method="post" enctype="multipart/form-data">
    <?php echo $this->session->flashdata(); ?>
    <fieldset class="form-group">
        <input type="hidden" name="txt_hidden" value="" class="form-control"> 
    </fieldset>
    <fieldset class="form-group">
        <label class="control-label" for="formGroupExampleInput">Add Main Caregory</label>
        <input type="text" value="<?php echo set_value('p_name'); ?>" placeholder="Main Category" name="p_name" class="form-control"> 
    </fieldset>
    <fieldset class="form-group">
        <label class="control-label"  for="formGroupExampleInput2">Order</label>
        <input type="text" name="order_id" placeholder="Order Id" value="<?php echo set_value('order_id'); ?>" class="form-control" id="formGroupExampleInput2"> 
    </fieldset>
    <fieldset class="form-group">
        <label class="control-label"  for="formGroupExampleInput2">Status ( 0 active , 1 inactive)</label>
        <input type="text" name="status" placeholder="Status" value="<?php echo set_value('status'); ?>" class="form-control" id="formGroupExampleInput2"> 
    </fieldset>
    <fieldset class="form-group">
        <label class="control-label"  for="formGroupExampleInput2">Image</label>
        <input type="file" name="p_image" placeholder="Image" value="<?php echo set_value('image'); ?>" class="form-control">
    </fieldset>
    <div class="form-group">
        <button type="submit" class="btn btn-primary">Add Category</button>
    </div>
</form>

废弃main_uploadmain_image_upload功能并使用此功能:

我们的新函数插件使用form_validation库来确保我们的字段是必需的并且设置正确,您可以根据自己的喜好调整规则,该类的完整文档是here

如果我们的验证失败,我们会添加一条Flash消息(请注意:我对CI的flash消息没有多少经验,可能需要进行一些调整)。

我们使用CI uploader上传图片。如果失败,我们会重定向到带有flash消息的页面。

所有其他通过,而不是我们添加到数据库。

public function __construct()
{
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->library('session');
}

public function insert()
{
    // validate
    $this->form_validation->set_rules('p_name', 'Main Category', 'required');
    $this->form_validation->set_rules('order_id', 'Order Id', 'required');
    $this->form_validation->set_rules('status', 'Status', 'required|in_list[1,0]');
    if ($this->form_validation->run() == FALSE) {
        $this->session->set_flashdata('errors', validation_errors());
        redirect(base_url() . 'admin/main_cat', 'refresh');
    }
    // validation passes, upload file
    $config['upload_path'] = './uploads/main_cat/'; // upload will fail if this folder doesn't exist!
    $config['file_name'] = uniqid(rand());
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('p_image')) {
        $this->session->set_flashdata('errors', $this->upload->display_errors());
        redirect(base_url() . 'admin/main_cat', 'refresh');
    }
    // upload passes, insert
    $upload_data = $this->upload->data();
    // this should really go in a model!
    $data = array(
        'main_image' => 'uploads/main_cat/' . $upload_data['file_name'],
        'p_name'     => $this->input->post('p_name'),
        'order_id'   => $this->input->post('order_id'),
        'status'     => $this->input->post('status')
    );
    $this->db->insert('main_category', $data);
    $this->session->set_flashdata('success', 'Added!');
    redirect(base_url() . 'admin/main_cat', 'refresh');
}