我在更新图片时遇到问题。我创建了可以正常工作的图像上传,但是当我尝试使用其他已有图像的详细信息更新条目时,它将引发错误。有没有一种方法可以完全跳过图像上传检查,并且无论有无图像,条目都会更新。
是代码点火器的新手。
查看:
<!-- begin .app -->
<div class="app">
<!-- begin .app-wrap -->
<div class="app-wrap">
<!-- begin .app-heading -->
<?php require_once("header.php"); ?>
<!-- END: .app-heading -->
<!-- begin .app-container -->
<div class="app-container">
<?php require_once("sidebar.php"); ?>
<!-- begin .app-side -->
<!-- END: .app-side -->
<!-- begin .app-main -->
<div class="app-main">
<!-- begin .main-heading -->
<header class="main-heading shadow-2dp">
<!-- begin dashhead -->
<div class="dashhead bg-white">
<div class="dashhead-titles">
<h3 class="dashhead-title">Add Mick Chocolate</h3>
</div>
<div class="dashhead-toolbar">
<div class="dashhead-toolbar-item">
<a href="">Chocolate</a>
/ Home / Add Mick Chocolate
</div>
</div>
</div>
<!-- END: dashhead -->
</header>
<!-- END: .main-heading -->
<!-- begin .main-content -->
<div class="main-content bg-clouds">
<!-- begin .container-fluid -->
<div class="container-fluid p-t-15">
<div class="box">
<header class="bg-alizarin text-white">
<h4>Input</h4>
</header>
<div class="box-body collapse in">
<?php
echo form_open_multipart('Admin/admin_add_image');
?>
<form class="form-horizontal" action = "" method ="post">
<hr class="b-s-dashed">
<div class="form-group">
<label for="text1" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" id="text1" name="chocolate_name" pattern="^[A-Za-z -]+$" required >
</div>
</div><br><br>
<div class="form-group">
<label for="text2" class="col-sm-2 control-label">Price</label>
<div class="col-sm-10">
<input type="text" id="text2" name="chocolate_price" pattern="^(0|[1-9][0-9]*)$" required >
</div>
</div><br><br>
<div class="form-group">
<label for="text3" class="col-sm-2 control-label">Upload Image</label>
<div class="col-sm-10">
<input type="file" id="text3" name="file_to_upload" accept="image/gif, image/jpeg, image/png, image/jpg">
</div>
</div><br><br>
<button type="submit" name="btn_add" class="btn btn-info">Add</button>
</form>
</div>
<!-- END: .container-fluid -->
</div>
<!-- END: .main-content -->
<!-- begin .main-footer -->
<footer class="main-footer bg-white p-a-5">
</footer>
<!-- END: .main-footer -->
</div>
<!-- END: .app-main -->
</div>
<!-- END: .app-container -->
<!-- begin .app-footer -->
<?php require_once("footer.php"); ?>
<!-- END: .app-footer -->
</div>
<!-- END: .app-wrap -->
</div>
<!-- END: .app -->
**Model**
```php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AdminModel extends CI_Model {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function admin_register_done()
{
$admin_name=$this->input->post('admin_name');
$admin_email=$this->input->post('admin_email');
$admin_mobileno=$this->input->post('admin_mobileno');
$admin_username=$this->input->post('admin_username');
$admin_password=$this->input->post('admin_password');
$data=array(
'admin_name'=>$admin_name,
'admin_email'=>$admin_email,
'admin_mobileno'=>$admin_mobileno,
'admin_username'=>$admin_username,
'admin_password'=>$admin_password
);
$this->db->insert('tbl_admin_registration',$data);
}
public function admin_login_done()
{
$admin_username=$this->input->post('admin_username');
$admin_password=$this->input->post('admin_password');
$this->db->select('admin_username','admin_password');
$this->db->from('tbl_admin_registration');
$this->db->where('admin_username',$admin_username);
$this->db->where("admin_password",$admin_password);
return $query=$this->db->get();
}
public function admin_add_image_done($image)
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>'http://candymart.co.in/Admin/uploads/'.$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->insert('tbl_admin_chocolate',$data);
}
public function admin_add_image_done1()
{
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->insert('tbl_admin_chocolate',$data);
}
public function admin_add_madhurimage_done()
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->insert('tbl_admin_madhurchocolate',$data);
}
public function admin_add_crystalimage_done()
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->insert('tbl_admin_crystalchocolate',$data);
}
public function admin_add_candyimage_done()
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->insert('tbl_admin_candychocolate',$data);
}
public function admin_display_image_done()
{
$this->db->select('*');
$this->db->from('tbl_admin_chocolate');
$query=$this->db->get();
return $query->result();
}
public function admin_display_madhurimage_done()
{
$this->db->select('*');
$this->db->from('tbl_admin_madhurchocolate');
$query=$this->db->get();
return $query->result();
}
public function admin_display_crystalimage_done()
{
$this->db->select('*');
$this->db->from('tbl_admin_crystalchocolate');
$query=$this->db->get();
return $query->result();
}
public function admin_display_candyimage_done()
{
$this->db->select('*');
$this->db->from('tbl_admin_candychocolate');
$query=$this->db->get();
return $query->result();
}
public function admin_delete_image_done($id)
{
$this->db->where('tbl_admin_chocolate.id',$id);
return $this->db->delete('tbl_admin_chocolate');
}
public function admin_delete_madhurimage_done($id)
{
$this->db->where('tbl_admin_madhurchocolate.id',$id);
return $this->db->delete('tbl_admin_madhurchocolate');
}
public function admin_delete_crystalimage_done($id)
{
$this->db->where('tbl_admin_crystalchocolate.id',$id);
return $this->db->delete('tbl_admin_crystalchocolate');
}
public function admin_delete_candyimage_done($id)
{
$this->db->where('tbl_admin_candychocolate.id',$id);
return $this->db->delete('tbl_admin_candychocolate');
}
public function admin_edit_chocolate_done($id)
{
$query = $this->db->get_where('tbl_admin_chocolate',array('id'=>$id));
return $query->row_array();
}
public function admin_edit_madhurchocolate_done($id)
{
$query = $this->db->get_where('tbl_admin_madhurchocolate',array('id'=>$id));
return $query->row_array();
}
public function admin_edit_crystalchocolate_done($id)
{
$query = $this->db->get_where('tbl_admin_crystalchocolate',array('id'=>$id));
return $query->row_array();
}
public function admin_edit_candychocolate_done($id)
{
$query = $this->db->get_where('tbl_admin_candychocolate',array('id'=>$id));
return $query->row_array();
}
public function admin_update_chocolate_done($id)
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>'http://candymart.co.in/Admin/uploads/'.$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->set($data);
$this->db->where('tbl_admin_chocolate.id',$id);
return $this->db->update('tbl_admin_chocolate',$data);
}
public function admin_update_madhurchocolate_done($id)
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->set($data);
$this->db->where('tbl_admin_madhurchocolate.id',$id);
return $this->db->update('tbl_admin_madhurchocolate',$data);
}
public function admin_update_crystalchocolate_done($id)
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->set($data);
$this->db->where('tbl_admin_crystalchocolate.id',$id);
return $this->db->update('tbl_admin_crystalchocolate',$data);
}
public function admin_update_candychocolate_done($id)
{
$image_info = $this->upload->data();
$chocolate_name=$this->input->post('chocolate_name');
$chocolate_price=$this->input->post('chocolate_price');
$data=array(
'image_name'=>$image_info['file_name'],
'chocolate_name'=>$chocolate_name,
'chocolate_price'=>$chocolate_price
);
$this->db->set($data);
$this->db->where('tbl_admin_candychocolate.id',$id);
return $this->db->update('tbl_admin_candychocolate',$data);
}
}
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('manisha_admin');
}
public function admin_register()
{
$this->load->view('admin_register');
}
public function admin_register_data()
{
$this->load->model('AdminModel');
$this->AdminModel->admin_register_done();
$this->load->view('admin_login');
}
public function admin_login()
{
$this->load->view('admin_login');
}
public function admin_login_data()
{
$this->load->model('AdminModel');
$this->AdminModel->admin_login_done();
$data['sub'] = $this->AdminModel->admin_display_image_done();
$this->load->view('add_image',$data);
}
public function admin_add_image()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
//$image = 'http://candymart.co.in/Admin/uploads/1.JPG';
$this->load->view('add_image');
}
else {
$data = array('upload_data' => $this->upload->data());
//$image = $_FILES['file_to_upload']['name'];
$this->load->view('Upload_success', $data);
$this->load->model('AdminModel');
$this->AdminModel->admin_add_image_done();
}
}
public function admin_add_madhurimage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_madhurimage', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('Upload_success', $data);
$this->load->model('AdminModel');
$this->AdminModel->admin_add_madhurimage_done();
}
}
public function admin_add_crystalimage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_crystalimage', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('Upload_success', $data);
$this->load->model('AdminModel');
$this->AdminModel->admin_add_crystalimage_done();
}
}
public function admin_add_candyimage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_candyimage', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('Upload_success', $data);
$this->load->model('AdminModel');
$this->AdminModel->admin_add_candyimage_done();
}
}
public function admin_edit_image()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('edit_chocolate', $error);
}
else {
$id = $this->input->post('hidden_id1');
$data = array('upload_data' => $this->upload->data());
//$this->load->view('Upload_success',$data);
$this->load->model('AdminModel');
$this->AdminModel->admin_update_chocolate_done($id);
$data['sub'] = $this->AdminModel->admin_display_image_done();
$this->load->view('display_image',$data);
}
}
public function admin_edit_madhurimage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('edit_madhurchocolate', $error);
}
else {
$id = $this->input->post('hidden_id2');
$data = array('upload_data' => $this->upload->data());
//$this->load->view('Upload_success',$data);
$this->load->model('AdminModel');
$this->AdminModel->admin_update_madhurchocolate_done($id);
$data['sub'] = $this->AdminModel->admin_display_madhurimage_done();
$this->load->view('display_madhurimage',$data);
}
}
public function admin_edit_crystalimage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('edit_crystalchocolate', $error);
}
else {
$id = $this->input->post('hidden_id3');
$data = array('upload_data' => $this->upload->data());
//$this->load->view('Upload_success',$data);
$this->load->model('AdminModel');
$this->AdminModel->admin_update_crystalchocolate_done($id);
$data['sub'] = $this->AdminModel->admin_display_crystalimage_done();
$this->load->view('display_crystalimage',$data);
}
}
public function admin_edit_candyimage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file_to_upload')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('edit_candychocolate', $error);
}
else {
$id = $this->input->post('hidden_id4');
$data = array('upload_data' => $this->upload->data());
//$this->load->view('Upload_success',$data);
$this->load->model('AdminModel');
$this->AdminModel->admin_update_candychocolate_done($id);
$data['sub'] = $this->AdminModel->admin_display_candyimage_done();
$this->load->view('display_candyimage',$data);
}
}
public function admin_display_image()
{
$this->load->model('AdminModel');
$data['sub'] = $this->AdminModel->admin_display_image_done();
$this->load->view('display_image',$data);
}
public function admin_display_madhurimage()
{
$this->load->model('AdminModel');
$data['sub'] = $this->AdminModel->admin_display_madhurimage_done();
$this->load->view('display_madhurimage',$data);
}
public function admin_display_crystalimage()
{
$this->load->model('AdminModel');
$data['sub'] = $this->AdminModel->admin_display_crystalimage_done();
$this->load->view('display_crystalimage',$data);
}
public function admin_display_candyimage()
{
$this->load->model('AdminModel');
$data['sub'] = $this->AdminModel->admin_display_candyimage_done();
$this->load->view('display_candyimage',$data);
}
public function admin_delete_image()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$this->AdminModel->admin_delete_image_done($id);
$data['sub'] = $this->AdminModel->admin_display_image_done();
$this->load->view('display_image',$data);
}
public function admin_delete_madhurimage()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$this->AdminModel->admin_delete_madhurimage_done($id);
$data['sub'] = $this->AdminModel->admin_display_madhurimage_done();
$this->load->view('display_madhurimage',$data);
}
public function admin_delete_crystalimage()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$this->AdminModel->admin_delete_crystalimage_done($id);
$data['sub'] = $this->AdminModel->admin_display_crystalimage_done();
$this->load->view('display_crystalimage',$data);
}
public function admin_delete_candyimage()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$this->AdminModel->admin_delete_candyimage_done($id);
$data['sub'] = $this->AdminModel->admin_display_candyimage_done();
$this->load->view('display_candyimage',$data);
}
public function admin_edit_chocolate()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$data['user1']=$this->AdminModel->admin_edit_chocolate_done($id);
$this->load->view('edit_chocolate',$data);
}
public function admin_edit_madhurchocolate()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$data['user2']=$this->AdminModel->admin_edit_madhurchocolate_done($id);
$this->load->view('edit_madhurchocolate',$data);
}
public function admin_edit_crystalchocolate()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$data['user3']=$this->AdminModel->admin_edit_crystalchocolate_done($id);
$this->load->view('edit_crystalchocolate',$data);
}
public function admin_edit_candychocolate()
{
$id = $this->uri->segment(3);
$this->load->model('AdminModel');
$data['user4']=$this->AdminModel->admin_edit_candychocolate_done($id);
$this->load->view('edit_candychocolate',$data);
}
public function admin_update_chocolate()
{
$id = $this->input->post('hidden_id1');
$this->load->model('AdminModel');
$this->AdminModel->admin_update_chocolate_done($id);
}
public function admin_update_madhurchocolate()
{
$id = $this->input->post('hidden_id2');
$this->load->model('AdminModel');
$this->AdminModel->admin_update_madhurchocolate_done($id);
}
public function admin_update_crystalchocolate()
{
$id = $this->input->post('hidden_id3');
$this->load->model('AdminModel');
$this->AdminModel->admin_update_crystalchocolate_done($id);
}
public function admin_update_candychocolate()
{
$id = $this->input->post('hidden_id4');
$this->load->model('AdminModel');
$this->AdminModel->admin_update_candychocolate_done($id);
}
public function admin_logout()
{
$user_data = $this->session->all_userdata();
foreach ($user_data as $key => $value) {
if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
$this->session->unset_userdata($key);
}
}
$this->session->sess_destroy();
redirect('Admin/index');
}
}
答案 0 :(得分:0)
您可以使用:
<?php
....
if($_FILES["file_name"]["name"] != "") {
//file uploading code
}
....
?>
答案 1 :(得分:0)
在更新时在控制器中尝试
if(isset($_FILES['file_to_upload']['name'])){
//Image uploading...
}
//Updating other data...
答案 2 :(得分:0)
我注意到您已经两次书写表格标签。查看视图
<?php
echo form_open_multipart('Admin/admin_add_image');
?>
<form class="form-horizontal" action = "" method ="post">
首先删除其中一个,然后尝试再次上传图片。
答案 3 :(得分:0)
您的视图文件HTML表单应具有enctype属性才能上传文件。
<form action="" method="post" enctype="multipart/form-data">
</form>
答案 4 :(得分:0)
当更新所有详细信息时,请在控制器中检查这种情况。
In [56]: sympify("(2*x + 3*y + 1)*(4*x**2 - 6*x*y - 2*x + 9*y**2 - 3*y + 1)")
Out[56]:
⎛ 2 2 ⎞
(2⋅x + 3⋅y + 1)⋅⎝4⋅x - 6⋅x⋅y - 2⋅x + 9⋅y - 3⋅y + 1⎠