dsaxw4201 2015-09-17 20:28
浏览 24
已采纳

除了一个,我可以获得所有$ _POST数据

I'm trying to input a record into my database. I can get all data except for one ['inputPicture'](which is the picture file name)

I've already tried using var_dum($_POST) and print_r($_POST) so I am sure that I cannot get anything from 'inputPicture'

This is what I get from both of them:

Array ( [inputFirstName] => Charlie [inputLastName] => Horse [inputContactNumber] => 09154447896 [inputAddress] => Candy Mountain [inputEmailAddress] => charlie@candymountain.com [action] => )

Here is my view code:

<form class="form-horizontal" role="form" method="POST" action="<?php echo base_url('Contacts/addcontact'); ?>" enctype="multipart/form-data">
                        <div class="form-group">
                          <label for="usr">First Name</label>
                          <input type="text" class="form-control" name="inputFirstName" placeholder="First Name">
                        </div>
                        <div class="form-group">
                          <label for="usr">Last Name</label>
                          <input type="text" class="form-control" name="inputLastName" placeholder="Last Name">
                        </div>
                        <div class="form-group">
                          <label for="usr">Contact Number</label>
                          <input type="text" class="form-control" name="inputContactNumber" placeholder="Contact Number">
                        </div>
                        <div class="form-group">
                          <label for="usr">Address:</label>
                          <input type="text" class="form-control" name="inputAddress" placeholder="Address">
                        </div>
                        <div class="form-group">
                          <label for="usr">Email Address:</label>
                          <input type="text" class="form-control" name="inputEmailAddress" placeholder="Email Address">
                        </div>
                        <div class="form-group">
                            <label for="usr">Picture:</label>
                            <input type="file" class="form-control" name="inputPicture"></br>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                          <button type="submit" class="btn btn-primary" name="action">Add</button>
                        </div>
                    </form>

Here is my controller code:

public function addcontact(){
    $first_name = $this->input->post('inputFirstName');
    $last_name = $this->input->post('inputLastName');
    $contact_number = $this->input->post('inputContactNumber');
    $address = $this->input->post('inputAddress');
    $email_address = $this->input->post('inputEmailAddress');
    $image_url = $this->input->post('inputImage');

    $this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
    $this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
    $this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
    $this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
    $this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');

    if($this->form_validation->run() == FALSE){
        $data['title'] = 'Address Book';
        $data['contacts_info'] = $this->contacts_model->getContacts();
        $this->load->view('home', $data);
        redirect();
    }
    else{
        if(!isset($_POST['inputImage'])){
            $this->contacts_model->addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address);
        }
        else{
            $image = 'assets/images/' . $image_url;
            $this->contacts_model->addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image);
        }

        $data['title'] = 'Address Book';
        $data['contacts_info'] = $this->contacts_model->getContacts();
        $this->load->view('home', $data);
        redirect();
    }
}

I have a similar method which is 'update'. It works perfectly fine. I tried copy pasting my code from the update to my addContact method but it still doesnt work.

Here is my view code for Update:

<form class="form-horizontal" role="form" method="POST" action="<?php echo base_url('Contacts/update'); ?>" enctype="multipart/form-data">
                        <div class="form-group hidden">
                          <label for="usr">ID</label>
                          <input type="text" class="form-control" name="inputID" id="id">
                        </div>
                        <div class="form-group">
                          <label for="usr">First Name</label>
                          <input type="text" class="form-control" name="inputFirstName" id="firstname">
                        </div>
                        <div class="form-group">
                          <label for="usr">Last Name</label>
                          <input type="text" class="form-control" name="inputLastName" id="lastname">
                        </div>
                        <div class="form-group">
                          <label for="usr">Contact Number</label>
                          <input type="text" class="form-control" name="inputContactNumber" id="contactnumber">
                        </div>
                        <div class="form-group">
                          <label for="usr">Address:</label>
                          <input type="text" class="form-control" name="inputAddress" id="address">
                        </div>
                        <div class="form-group">
                          <label for="usr">Email Address:</label>
                          <input type="text" class="form-control" name="inputEmailAddress" id="emailaddress">
                        </div>
                        <div class="form-group">
                            <label for="usr">Picture:</label>
                            <input type="file" class="form-control" name="inputPicture" id="picture"></br>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                          <button type="submit" class="btn btn-primary" name="action">Update</button>
                        </div>
                    </form>

and here is my controller

 public function update(){
    $first_name = $this->input->post('inputFirstName');
    $last_name = $this->input->post('inputLastName');
    $contact_number = $this->input->post('inputContactNumber');
    $address = $this->input->post('inputAddress');
    $email_address = $this->input->post('inputEmailAddress');
    $image_url = $this->input->post('inputPicture');
    $id = $this->input->post('inputID');
    var_dump($_POST);exit;  
    $this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
    $this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
    $this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
    $this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
    $this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');

    if($this->form_validation->run() == FALSE){
        $data['title'] = 'Address Book';
        $data['contacts_info'] = $this->contacts_model->getContacts();
        $this->load->view('home', $data);
        redirect();
    }
    else{
        if(!isset($_POST['inputPicture'])){
            $this->contacts_model->updateContactNoPic($id, $first_name, $last_name, $contact_number, $address, $email_address);
        }
        else{
            $image = 'assets/images/' . $image_url;
            $this->contacts_model->updateContact($id, $first_name, $last_name, $contact_number, $address, $email_address, $image);
        }

        $data['title'] = 'Address Book';
        $data['contacts_info'] = $this->contacts_model->getContacts();
        $this->load->view('home', $data);
        redirect();
    }
}
  • 写回答

2条回答 默认 最新

  • douteng5673 2015-09-17 20:34
    关注

    That's because contents of input type=file sent by GET/POST methods from HTML to PHP are stored inside the superglobal variable $_FILES and not $_POST (unless you don't define enctype property of the form tag as "multipart/form-data", which then causes the filename to be passed as a string to GET/POST).

    If you var_dump($_FILES)/print_r($_FILES) you'll see an array like this:

    Array
    (
        [file] => Array
            (
                [name] => test.pdf
                [type] => application/pdf
                [tmp_name] => C:\Windows\Temp\php1485.tmp
                [error] => 0
                [size] => 1073054
            )
    )
    

    OBS: be sure to have enctype="multipart/form-data" as property of your form and file_uploads set to on in your php.ini file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 软件工程用例图的建立(相关搜索:软件工程用例图|画图)
  • ¥15 如何在arcgis中导出拓扑关系表
  • ¥15 处理数据集文本挖掘代码
  • ¥15 matlab2017
  • ¥15 在vxWorks下TCP/IP编程,总是connect()报错,连接服务器失败: errno = 0x41
  • ¥15 AnolisOs7.9如何安装 Qt_5.14.2的运行库
  • ¥20 求:怎么实现qt与pcie通信
  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3