I'm trying to insert a new image into the database, but I got this error. Could someone help me please?
Here is my Controller:
public function tambahfotoproses()
{
$variable = 'foto'; //variable name dari form
$directory = './assets/images/dokter/'; //direktori
$allowed_file = 'gif|jpg|jpeg|png|JPG|GIF|PNG|JPEG'; //file yang diizinkan dibatasi dengan tanga |
$upload['detail'] = $this->m_admin->uploadfile($variable,$directory,$allowed_file); //proses dengan modul insertfile
/* to upload file */
$filename = $upload['detail']['file_name'];
if ($filename != "")
{
$data = array(
'foto' => $filename,
'person_nm' => $this->input->post('person_nm'),
'pengupload' => $this->input->post('pengupload'),
'tgl' => $this->input->post('tgl')
);
$insert = $this->db->insert('eregister_foto',$data);
} else {
$data = array(
'foto' => $filename,
'person_nm' => $this->input->post('person_nm'),
'pengupload' => $this->input->post('pengupload'),
'tgl' => $this->input->post('tgl')
);
$insert = $this->db->insert('eregister_foto',$data);
}
redirect('index.php/admin/foto_dr');
}
public function aksi_upload(){
//membuat konfigurasi
$config = [
'upload_path' => './assets/images/dokter',
'allowed_types' => 'gif|jpg|jpeg|png|JPG|GIF|PNG|JPEG',
'max_size' => 1000, 'max_width' => 1000,
'max_height' => 1000
];
$this->load->library('upload', $config);
if (!$this->upload->do_upload('foto'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('eregister_foto', $error);
} else
{
$file = $this->upload->data();
//mengambil data di form
$data = ['foto' => $file['file_name'],
'person_nm' => set_value('person_nm'),
'pengupload' => set_value('pengupload'),
'tgl' => set_value('tgl')
];
$insert = $this->db->insert('eregister_foto',$data);
redirect('index.php/admin/foto_dr');
}
}
I am trying to build with codeigniter, here is my Modals.php I made this modals to change the name of the inputted photo
/AKSI UPLOAD FILE/
function uploadfile($var,$dir,$all){
$new_name = $this->input->post('person_nm');
$config2=array(
'image_library' => 'gd2',
'upload_path' => $dir."/dokter/", //lokasi gambar akan di simpan
'allowed_types' => $all, //ekstensi gambar yang boleh di unggah
'create_thumb' => TRUE,
'max_size' => '2048', //batas maksimal ukuran gambar
'file_name' => $new_name
);
$this->load->library('upload');
$this->upload->initialize($config2);
if ($this->upload->do_upload($var))
{
return $this->upload->data();
}
else
{
return 0;
}
}
Error Number: 1048
Column 'foto' cannot be null
here is my view.php :
<div class="modal fade" id="tambahdata">
<div class="modal-dialog">
<div class="modal-content">
<form action="<?php base_url(); ?>tambahfotoproses" method="post" enctype="multipart/form-data">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title">Tambah Data Jam Kedatangan</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>Pilih Dokter</label>
<select class="form-control select2" name="person_nm" style="width: 100%;">
<option selected="selected">- Pilih Dokter -</option>
<?php foreach ($xocp_persons as $nama_dokter): ?>
<option value="<?php echo $nama_dokter['person_nm']; ?>"><?php echo $nama_dokter['person_nm']; ?></option>
<?php endforeach; ?>
</select>
</div>
<input type="text" name="pengupload" value="<?php echo $this->session->userdata("user_nm"); ?>" hidden>
<input type="text" name="tgl" value="<?php date_default_timezone_set('Asia/Jakarta'); echo date('Y-m-d h:i:s'); ?>" hidden>
<div class="form-group">
<label for="exampleInputFile">Upload Foto</label>
<input type="file" id="inputfile" name="foto">
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#inputfile").change(function() {
readURL(this);
});
</script>
</div>
<div class="form-group">
<img id="blah" src="" width="150px">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger pull-left" data-dismiss="modal">Batal</button>
<button type="submit" class="btn btn-success">Simpan</button>
</div>
</form>
</div>
<!-- /.modal-content -->
</div>
</div>