我的型号:
class Admin_model extends CI_Model{
function add_blog($data){
$this->db->insert('blog',$data);
return true;
}
}
我的控制器功能:
function add_blog(){
$this->form_validation->set_rules('heading','Heading', 'trim|required');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error',"Please check the required field");
redirect('admin/dashboard/add');
}else{
//for upload file
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'pdf|doc|docx|txt|jpeg|png|ppt';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$image_path = base_url("uploads/".$post['raw_name'].$post['file_ext']);
$data['picture'] = $image_path;
$data_image = array('upload_data' => $this->upload->data());
}
//Upload file end
$data['picture'] = $data_image['upload_data']['file_name'];
$data['heading'] = ucwords($this->input->post('heading'));
$data['description'] = ucfirst($this->input->post('description'));
$data['type_id'] = ucwords($this->input->post('type_id'));
$data['price'] = $this->input->post('price');
$insert_data = $this->admin_model->add_blog($data);
if($insert_data){
echo '<script>alert("Note added sucessfully.");</script>';
redirect('admin/dashboard');
}else{
$this->session->set_flashdata('message',"There is problem in inserting data, please retry once");
echo "error at last ";
redirect('admin/dashboard/add');
}
}
}
我的观点:
<form role="form" name="myForm" method="post" enctype="multipart/form-data" action="<?php echo site_url('admin/dashboard/add_blog');?>">
<label for="">Category</label>
<select name="type_id" id="" class="form-control" >
<?php
if(count($product_list )>0 ){
foreach ($product_list as $key => $value) {
?>
<option value="<?php echo $value['type_id'];?>"><?php echo $value['type'];?></option>
<?php
}}?>
</select>
<label for=""> Heading </label>
<input type="text" name="heading"class="form-control">
<label for="">Price</label>
<input type="number" name="price" class="form-control">
<label for="">Description</label>
<textarea name="description" id="" cols="30" rows="5" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="exampleInputFile">Upload pic</label>
<div class="input-group">
<div class="custom-file">
<!-- <input type="file" name="userfile" class="form-control"> -->
<?php echo form_upload(['name'=>'userfile']); ?>
</div>
</div>
<?php if(isset($upload_error)){echo $upload_error;}?>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
遇到PHP错误
严重性:通知消息:未定义的变量:data_image文件名:admin/Dashboard.php行号:64
回溯:
文件:/opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard.php行:64函数:_error_handler
文件:/opt/lamp/htdocs/shremad/index.php行:315函数:需要一次
遇到PHP错误
严重性:通知消息:正在尝试访问类型为null的值的数组偏移量文件名:admin/Dashboard.php行号:64
回溯:
文件:/opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard.php行:64函数:_error_handler
文件:/opt/lamp/htdocs/shremad/index.php行:315函数:需要一次
发生数据库错误
错误编号:1048列"picture"不能为空
插入
blog
(picture
、heading
、description
、type_id
,price
(值(NULL,'Djjdj','Jjdjj','1',''(文件名:models/Admin_model.php线路编号:5
让我们首先分析错误消息
消息:未定义的变量:data_image
这意味着,在代码的某个地方,您正在调用一个未定义的名为"data_image"的变量。现在,请注意,这是一个通知,所以在某些情况下,您的代码可能会很好地工作,但这是一种糟糕的做法,可能会引起麻烦。(比如你的情况(。
看看你的代码,下面的"块":
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$image_path = base_url("uploads/".$post['raw_name'].$post['file_ext']);
$data['picture'] = $image_path;
$data_image = array('upload_data' => $this->upload->data());
}
//Upload file end
$data['picture'] = $data_image['upload_data']['file_name'];
$data_image
变量仅在"else
"语句下定义。因此,如果条件返回true,则$data_image
变量确实未定义。在这种情况下,以下命令:
$data['picture'] = $data_image['upload_data']['file_name'];
基于$data_image
变量的将返回另一个问题:
消息:试图访问类型为null 的值的数组偏移量
所以$data['picture']
现在是NULL
,这导致了您的最后一个问题:
列"picture"不能为空
解决方案
即使条件返回true,也要确保$data_image有一个值。或者,如果此条件返回true,请以不同的方式处理问题。还请注意,返回意外结果的条件中的函数可能存在逻辑问题。
(条件:if ( ! $this->upload->do_upload())
(