I am using Codeigniter and trying to upload image name with username in database. In my view file there are two fields one input field and second one file type input field. But there is problem when I am trying to pass first input field value from one function to another its shows blank. here is my code...
controller
class img_upload extends CI_Controller
{
public function index()
{
$data['title'] = 'Image Upload & Display';
$this->load->view('img_upload_view',$data);
}
public function check_img()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('userfile', 'Userfile', 'callback_image_validation');
if($this->form_validation->run() == false)
{
$this->index();
}
else
{
$data['name'] = $this->input->post('name');
$this->image_validation($data['name']);
}
}
public function image_validation($data)
{
$name = $data['name'];
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = $this->upload->display_errors();
$this->form_validation->set_message('image_validation', $error);
return false;
}
else
{
$data = $this->upload->data();
$img = $data['file_name'];
$data = array(
'name' => $name,
'img_name' => $img
);
$this->load->model('img_model');
$this->img_model->Insert_Data($data);
$this->session->set_flashdata('success', "Data Inserted Successfully!");
return true;
}
}
}
model
class img_model extends CI_Model
{
public function Insert_Data($data)
{
$this->db->insert('image', $data);
}
}
view
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-md-8">
<br>
<div class="card">
<div class="card-body">
<h5 class="card-title"><h2><?php echo $title; ?></h2></h5>
<p class="card-text">
<?php if($this->session->flashdata('success') == '')
{
echo $this->session->flashdata('success');
}
?>
<?php echo form_open_multipart('img_upload/check_img'); ?>
<div class="form-group">
<label for="exampleInputName">Name</label>
<input type="text" class="form-control" placeholder="Enter username" name="name">
<span><?php echo form_error('name');?></span>
</div>
<div class="form-group">
<label for="exampleInputUpload">Upload</label>
<input type="file" class="form-control" name="userfile">
<span><?php echo form_error('userfile');?></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br>
<div class="img-reponsive">
<!-- <img src="<?php #echo 'upload/'.$images->img_name; ?>" height="150" width="150"> -->
</div>
</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
error
A Database Error Occurred
Error Number: 1048
Column 'name' cannot be null
INSERT INTO `image` (`name`, `img_name`) VALUES (NULL, 'c4.jpg')
Filename: C:/xampp/htdocs/image_upload/system/database/DB_driver.php
Line Number: 691