将网站上传到网络服务器后,无法将数据插入数据库

时间:2015-11-12 05:56:08

标签: php codeigniter

我一直在努力确保代码在我上传其他表单之后能够完美地工作,我可以很好地工作,除了这个我想将数据上传到数据库的用户上传图像或没有数据应该能够插入.. 我需要帮助。

 function add_student(){
    $this->load->helper('url');
    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

    $this->form_validation->set_rules('fname', 'Student Firstname', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('mname', 'Student Middlename', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('lname', 'Student Lastname', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('gender', 'Choose The Gender', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('datepicker', 'Please pick a DOB', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('marital', 'Please Enter Marital Status', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('pob', 'Please Enter POB Residence', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('pobdistrict', 'Please Enter The POB District', 'required|min_length[2]|max_length[15]');

    $this->form_validation->set_rules('pobregion', 'Please Enter The POB Region', 'required|min_length[5]|max_length[15]');


    $this->form_validation->set_rules('residence', 'Please Enter The Residence', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('region', 'Please Enter The Region', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('district', 'Please Enter The District', 'required|min_length[5]|max_length[15]');


    $this->form_validation->set_rules('education', 'Please Enter The Education Level', 'required|min_length[5]|max_length[30]');

    $this->form_validation->set_rules('phone', 'Please Enter The Education Level', 'required|min_length[5]|max_length[15]');


        $fulpath ='../images/student_profile/';

        $config['upload_path'] = $fulpath;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = "2048000"; // Can be set to particular file size , here it is 2 MB(2048 Kb)
        $config['x_axis'] = "768";
        $config['y_axis'] ="1024";

        $this->load->library('image_lib', $config);


        $this->image_lib->initialize($config);

        $this->image_lib->resize();

        $this->image_lib->crop();


       if($this->form_validation->run() == FALSE)

       {
           $this->load->model('fetch_data');

           $data['schoolname'] = $this->fetch_data->school_data();

           $data['partnername'] = $this->fetch_data->partner_data();

           $data['main_content'] = '/student/addstudent';

           $this->load->view('includes/template',$data);

       } else {

           $this->load->library('upload', $config);

           if(!$this->upload->do_upload()){

               echo '<h4 style="color: red;">

                    Failure! </h4>

                    <p style="color: red;">'.$this->upload->display_errors().'</p>';

               $data = array(

                   'student_fname' => $this->input->post('fname'),
                   'student_mname' => $this->input->post('mname'),
                   'student_lname' => $this->input->post('lname'),
                   'gender' => $this->input->post('gender'),
                   'dob' => $this->input->post('datepicker'),
                   'marital_status' => $this->input->post('marital'),
                   'education' => $this->input->post('education'),

                   'collage_id' =>$this->input->post('sname'),

                   'partner_id' =>$this->input->post('partner'),

                   'pob' => $this->input->post('pob'),
                   'region' => $this->input->post('pobregion'),
                   'district' => $this->input->post('pobdistrict'),

                   'residence' => $this->input->post('residence'),
                   'res_district' => $this->input->post('district'),

                   'res_region' => $this->input->post('region'),
                   'phone' => $this->input->post('phone'),

               );

               $this->load->model('insert_data');

               $this->insert_data->student_data($data);



           }

           else
           {
               $data_upload_files = $this->upload->data();

               $file_name =   $data_upload_files['file_name'];

               $image ='images/student_profile/'.$file_name;



               $data = array(
                   'student_fname' => $this->input->post('fname'),
                   'student_mname' => $this->input->post('mname'),
                   'student_lname' => $this->input->post('lname'),
                   'gender' => $this->input->post('gender'),
                   'dob' => $this->input->post('datepicker'),
                   'marital_status' => $this->input->post('marital'),
                   'education' => $this->input->post('education'),

                   'collage_id' =>$this->input->post('sname'),

                   'partner_id' =>$this->input->post('partner'),

                   'pob' => $this->input->post('pob'),
                   'region' => $this->input->post('pobregion'),
                   'district' => $this->input->post('pobdistrict'),

                   'residence' => $this->input->post('residence'),
                   'res_district' => $this->input->post('district'),

                   'res_region' => $this->input->post('region'),
                   'phone' => $this->input->post('phone'),

                   'student_profile_img' => $image ,
               );


               // Transfering Data To Model

               $this->load->model('insert_data');

               $this->insert_data->student_data($data);


           }

       }






    }

我从控制器传递数据的模型如下:

public function student_data($data){

    $this->db->insert('student_profile', $data);

    if($this->db->affected_rows() > 0)
    {
        // Code here after successful insert

        $data['main_content'] = '/student/success';

        $this->load->view('includes/template',$data);

        return true; // to the controller

    }else{

        $data['main_content'] = '/student/success';

        $this->load->view('adminhome',$data);

    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

在控制器中

function add_student()
{
    $this->load->helper('url');
    $this->load->model('insert_data');
    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $this->form_validation->set_rules('fname', 'Student Firstname', 'required|min_length[2]|max_length[35]');
    $this->form_validation->set_rules('mname', 'Student Middlename', 'required|min_length[2]|max_length[35]');
    $this->form_validation->set_rules('lname', 'Student Lastname', 'required|min_length[2]|max_length[35]');
    $this->form_validation->set_rules('gender', 'Choose The Gender', 'required|min_length[2]|max_length[35]');
    $this->form_validation->set_rules('datepicker', 'Please pick a DOB', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('marital', 'Please Enter Marital Status', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('pob', 'Please Enter POB Residence', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('pobdistrict', 'Please Enter The POB District', 'required|min_length[2]|max_length[15]');
    $this->form_validation->set_rules('pobregion', 'Please Enter The POB Region', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('residence', 'Please Enter The Residence', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('region', 'Please Enter The Region', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('district', 'Please Enter The District', 'required|min_length[5]|max_length[15]');
    $this->form_validation->set_rules('education', 'Please Enter The Education Level', 'required|min_length[5]|max_length[30]');
    $this->form_validation->set_rules('phone', 'Please Enter The Education Level', 'required|min_length[5]|max_length[15]');


    $fulpath ='../images/student_profile/';

    $config['upload_path'] = $fulpath;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = "2048000"; // Can be set to particular file size , here it is 2 MB(2048 Kb)
    $config['x_axis'] = "768";
    $config['y_axis'] ="1024";

    $this->load->library('image_lib', $config);

    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->crop();

    if($this->form_validation->run() == FALSE){

        echo "Form validation false";
       /*$this->load->model('fetch_data');
       $data['schoolname'] = $this->fetch_data->school_data();
       $data['partnername'] = $this->fetch_data->partner_data();
       $data['main_content'] = '/student/addstudent';
       $this->load->view('includes/template',$data);*/

    } 
    else {

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload())
        {
            echo '<h4 style="color: red;">
                Failure! </h4>
                <p style="color: red;">'.$this->upload->display_errors().'</p>';

            $data = array(
               'student_fname' => $this->input->post('fname'),
               'student_mname' => $this->input->post('mname'),
               'student_lname' => $this->input->post('lname'),
               'gender' => $this->input->post('gender'),
               'dob' => $this->input->post('datepicker'),
               'marital_status' => $this->input->post('marital'),
               'education' => $this->input->post('education'),
               'collage_id' =>$this->input->post('sname'),
               'partner_id' =>$this->input->post('partner'),
               'pob' => $this->input->post('pob'),
               'region' => $this->input->post('pobregion'),
               'district' => $this->input->post('pobdistrict'),
               'residence' => $this->input->post('residence'),
               'res_district' => $this->input->post('district'),
               'res_region' => $this->input->post('region'),
               'phone' => $this->input->post('phone'),
            );          

            $result = $this->insert_data->student_data($data);

            if ($result == TRUE) {
                echo "Success. (In top)";
            }
            else{
                echo "Faile. (In top)";
            }
        }
        else
        {
            $data_upload_files = $this->upload->data();
            $file_name =   $data_upload_files['file_name'];
            $image ='images/student_profile/'.$file_name;

            $data = array(
               'student_fname' => $this->input->post('fname'),
               'student_mname' => $this->input->post('mname'),
               'student_lname' => $this->input->post('lname'),
               'gender' => $this->input->post('gender'),
               'dob' => $this->input->post('datepicker'),
               'marital_status' => $this->input->post('marital'),
               'education' => $this->input->post('education'),
               'collage_id' =>$this->input->post('sname'),
               'partner_id' =>$this->input->post('partner'),
               'pob' => $this->input->post('pob'),
               'region' => $this->input->post('pobregion'),
               'district' => $this->input->post('pobdistrict'),
               'residence' => $this->input->post('residence'),
               'res_district' => $this->input->post('district'),
               'res_region' => $this->input->post('region'),
               'phone' => $this->input->post('phone'),
               'student_profile_img' => $image ,
            );

            $result = $this->insert_data->student_data($data);

            if ($result == TRUE) {
                echo "Success. (In bottom)";
            }
            else{
                echo "Faile. (In bottom)";
            }
        }
    }
}

在模型中

public function student_data($data){

    if ($this->db->insert('student_profile', $data)) {

        return TRUE;
    }
    else{

        return FALSE;
    }

}