gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:38:31 25 4
gpt4 key购买 nike

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

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

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

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

这是我的 View 代码:

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

这是我的 Controller 代码:

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 方法,但它仍然不起作用。

这是我的更新 View 代码:

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

这是我的 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();
}
}

最佳答案

这是因为通过 GET/POST 方法从 HTML 发送到 PHP 的 input type=file 的内容存储在超全局变量 $_FILES 而不是 $_POST (除非您没有将 form 标记的 enctype 属性定义为 "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" 作为表单的属性并将 file_uploads 设置为 on 在你的 php.ini 文件中。

关于php - 我可以获得所有 $_POST 数据,除了一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com