【问题标题】:I can get all $_POST data except for one我可以获得除一个之外的所有 $_POST 数据
【发布时间】:2015-12-14 20:36:24
【问题描述】:

我正在尝试将记录输入到我的数据库中。我可以获取除一个['inputPicture'](即图片文件名)之外的所有数据

我已经尝试过使用 var_dum($_POST) 和 print_r($_POST) 所以我确信我无法从 'inputPicture' 得到任何东西

这是我从他们俩那里得到的:

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

这是我的查看代码:

<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>

这是我的控制器代码:

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();
    }
}

我有一个类似的方法是“更新”。它工作得很好。我尝试将我的代码从更新复制粘贴到我的 addContact 方法,但它仍然不起作用。

这是我的更新视图代码:

<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>

这是我的控制器

 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();
    }
}

【问题讨论】:

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

这是因为通过 GET/POST 方法从 HTML 发送到 PHP 的 input type=file 的内容存储在超全局变量 $_FILES 而不是 $_POST 中(除非您没有定义 formenctype 属性标记为"multipart/form-data",然后将文件名作为字符串传递给GET/POST)。

如果你var_dump($_FILES)/print_r($_FILES) 你会看到这样的数组:

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

OBS:确保将enctype="multipart/form-data" 作为表单的属性,并在您的php.ini 文件中将file_uploads 设置为on

【讨论】:

  • 我试过 print_r($_FILES);但它显示了一个数组。如何只获取文件名?
  • $_FILES['name'] :)
  • 如果您想将 真实文件 移动到另一个文件夹来存储上传的文件,请阅读move_uploaded_file 函数
  • 他们确实有enctype="multipart/form-data" Alan。
【解决方案2】:

使用 $_FILES['inputPicture']

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');
$image_url = $_FILES['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($_FILES['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();
}

}

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2020-05-13
    • 2023-03-10
    相关资源
    最近更新 更多