【发布时间】:2016-05-18 23:01:11
【问题描述】:
我的控制器是,但是如果用户在表单中输入他/她的详细信息时没有上传任何图像,我如何在数据库中插入默认图像名称。
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Student extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('student_model');
$this->load->model('hostelinfo_model');
$this->load->model('options_model');
$this->load->model('upload_model');
$this->load->library('form_validation');
$this->load->library('upload');
}
public function index() {
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('student_id', 'Student ID', 'required|trim|xss_clean|callback_student_id_check');
$this->form_validation->set_rules('student_name', 'Student Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('father_name', 'Father Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('program_name', 'Progrem Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('mobile', 'Mobile No', 'required|trim|xss_clean|is_unique[student.mobile]|max_length[11]|min_length[11]');
$this->form_validation->set_rules('email', 'Email ID', 'trim|xss_clean|is_unique[student.email]|valid_email');
$this->form_validation->set_rules('hostel_name', 'Hostel Name', 'required|trim|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header1');
$this->load->view('book_hostel');
$this->load->view('footer');
} else {
$config['upload_path'] = 'uploads';
$config['allowed_types'] = 'jpeg|jpg|png';
$config['max_size'] = '2048';
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_name')) {
$image_data = "noimage.png";
$data['error'] = $this->upload->display_errors();
$this->load->view('header');
$this->load->view('book_hostel', $data);
$this->load->view('footer');
}else{
$this->form_validation->set_rules('image_name', 'Upload Image', 'trim|xss_clean');
if ($this->form_validation->run($this) == FALSE) {
$data['error'] = '';
$this->load->view('header1');
$this->load->view('book_hostel', $data);
$this->load->view('footer');
}
}
$image_data = $this->upload->data();
$data = array(
'image_name' => $image_data['file_name'],
'student_id' => $this->input->post('student_id'),
'student_name' => $this->input->post('student_name'),
'father_name' => $this->input->post('father_name'),
'program_name' => $this->input->post('program_name'),
'mobile' => $this->input->post('mobile'),
'email' => $this->input->post('email'),
'hostel_name' => $this->input->post('hostel_name')
);
$this->student_model->form_insert($data);
//$this->sendEmail();
$data['message'] = 'Data Inserted Successfully';
redirect('hostel');
}
}
}
【问题讨论】:
标签: php image codeigniter upload