我正在尝试将记录输入到我的数据库中。除了一个['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();
}
}
答案 0 :(得分:4)
这是因为GET / POST方法从HTML发送到PHP的input type=file
内容存储在超全局变量$_FILES
内,而不是$_POST
中(除非你没有&#39} ; t将enctype
标记的form
属性定义为"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
设置为php.ini文件中的on
。
答案 1 :(得分:0)
使用$ _FILES [&#39; inputPicture&#39;]
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();
}
}