Unknown column 'Array' in 'field list' while uploading image - php

This is my controller
<?php
class Avatar extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->config('avatar');
$this->load->library('upload');
}
public function index()
{
// Prepare data
$data = array(
'avatar' => $this->user->getAvatar($this->user->getId()),
'max_size' => $this->config->item('max_size'),
'max_width' => $this->config->item('max_width'),
'max_height' => $this->config->item('max_height'),
'allowed_types' => $this->config->item('allowed_types'),
);
// Load the avatar page
$content = $this->template->loadPage("avatar.tpl", $data);
$title = breadcumb(array(
"ucp" => lang("ucp"),
"ucp/avatar" => lang("change_avatar", "ucp")
));
// Put it in a content box
$this->template->box($title, $content, true, "modules/ucp/css/avatar.css");
}
public function upload()
{
$this->load->model("avatar_model");
// Prepare data
$config = array(
'max_size' => $this->config->item('max_size'),
'max_width' => $this->config->item('max_width'),
'max_height' => $this->config->item('max_height'),
'allowed_types' => $this->config->item('allowed_types'),
'encrypt_name' => $this->config->item('encrypt_name'),
'xss_clean' => $this->config->item('xss_clean')
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('avatar'))
$data = array('error' => $this->upload->display_errors());
else
{
$this->avatar_model->setAvatar(array('avatar' => $this->upload->data('file_name')));
$data = array('error' => 'Success');
}
$title = breadcumb(array(
"ucp" => lang("ucp"),
"ucp/avatar" => lang("change_avatar", "ucp"),
"ucp/avatar/upload" => lang("upload_avatar", "ucp"),
));
$page = $this->template->loadPage("upload.tpl", $data);
$this->template->box($title, $page, true);
}
}
and this is the model
<?php
class Avatar_model extends CI_Model
{
public function setAvatar($avatar)
{
$this->db->update('account_data', $avatar, array('id' => $this->user->getId()));
}
}
I have no idea what is wrong, the configs are just my preference right?
<?php
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 512; // KB
$config['max_width'] = 500;
$config['max_height'] = 500;
$config['encrypt_name'] = true;
$config['xss_clean'] = false;
this is what i get when i try to upload the image
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysqli/mysqli_driver.php
Line Number: 612
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE account_data SET avatar = Array WHERE id = '11'
Filename: C:\xampp\htdocs\system\database\DB_driver.php
Line Number: 328

Related

Codeigniter image name send to data base as name

is this thing do is possible?
I want to send data and image name to database please help me to fix this error
i this image text name not send to db thats why there was a error please help me to fix this error
this is my controller
public function upload_file()
{
$config['allowed_types'] = '*';
$config['file_name'] = $data-> 'filename';
$config['upload_path'] = './uploads/Ehi';
$config['encrypt_name'] = false;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image'))
{
print_r($this->upload->data());
$this->load->model('ehi_model');
$data = array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'activedays' => $this->input->post('activedays'),
'filename' => $data-> 'filename';
);
$this->ehi_model->upload_file($data);
redirect(base_url() ."Ehi/index");
}
else
{
print_r($this->upload->display_error());
}
}
and this is my model
function upload_file($data)
{
$this->db->insert('ehi', $data);
}
Where is this $data defined?
$config['file_name'] = $data-> 'filename';
But following change will work for you.
'filename' => $this->upload->data('file_name')
Reference
Use that.
$this->ehi_model->upload_file($this->upload->data('file_name'));

$directory result Null in codeigniter

I made a file upload in codeigniter, but in the controller I set $ directory = './assets/images/'; unreadable on vmware.
please help, here is my controller
public function tambahfotoproses()
{
$variable = 'foto'; //variable name dari form
$directory = './assets/images/'; //direktori
$allowed_file = 'gif|jpg|jpeg|png|JPG|GIF|PNG|JPEG'; //file yang diizinkan dibatasi dengan tanda
$upload['detail'] = $this->m_admin->uploadfile($variable,$directory,$allowed_file); //proses dengan modul insertfile
var_dump($upload); die();
/* 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');
}
I deliberately gave var_dump () to find out the results of $ upload
the result is array (1) {["detail"] => int (0)}
photo not uploaded
and here is my modals.php
/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;
}
}
i've try to rewrite folder "dokter" too, and still can't work

How to solve error Number: 1048 Column 'photo_pict' cannot be null?

I got this error:
A Database Error Occurred
Error Number: 1048
Column 'photo_pict' cannot be null
INSERT INTO `tester` (`photo_pict`, `press_pict`, `name_pict`) VALUES (NULL, NULL, NULL)
Filename: C:/xampp/htdocs/pretest/system/database/DB_driver.php
Line Number: 691
I'm using CodeIgniter and I'm a beginner programmer.
I was making a register form, all I have to do is just saving the input from the register form to the database. But every data has been saved to database except the data for saving photo to the database.
Code show here
Model :
function insert($data){
$data = array(
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'birthDate' => $data['birthDate'],
'gender' => $data['gender'],
'posisi' => $data['posisi'],
'media_name' => $data['media_name'],
'media_region' => $data['media_region'],
'media_ctgry' => $data['media_ctgry'],
'company_addrss' => $data['company_addrss'],
'website' => $data['website'],
'editor_email' => $data['editor_email'],
'office_phone' => $data['office_phone'],
'office_fax' => $data['office_fax'],
'personal_email' => $data['personal_email'],
'phone_number' => $data['phone_number'],
'working_email' => $data['working_email']
);
$this->db->insert('tester', $data);
}
function upload($data){
$gambar = array(
'photo_pict'=> $data['photo_pict'],
'press_pict'=> $data['press_pict'],
'name_pict'=> $data['name_pict']
);
$this->db->insert('tester', $gambar);
}
function proses_regist(){
return $this->db->insert('tester', $data);
}
}
My Controller :
public function __construct(){
parent :: __construct();
$this->load->model("Model_regist");
$this->load->helper(array('url','form'));
}
public function index(){
$this->load->view('home');
}
function register(){
$gambar = array(
'press_pict'=>$this->upload->data("photo_pict"),
'photo_pict'=>$this->upload->data("press_pict"),
'name_pict'=>$this->upload->data("name_pict")
);
$data = array(
'first_name' => $this->input->post("first_name"),
'last_name' => $this->input->post("last_name"),
'birthDate' => $this->input->post("birthDate"),
'gender' => $this->input->post("gender"),
'posisi' => $this->input->post("posisi"),
'media_name' => $this->input->post("media_name"),
'media_region' => $this->input->post("media_region"),
'media_ctgry' => $this->input->post("media_ctgry"),
'company_addrss' => $this->input->post("company_addrss"),
'website' => $this->input->post("website"),
'editor_email' => $this->input->post("editor_email"),
'office_phone' => $this->input->post("office_phone"),
'office_fax' => $this->input->post("office_fax"),
'personal_email' => $this->input->post("personal_email"),
'phone_number' => $this->input->post("phone_number"),
'working_email' => $this->input->post("working_email")
);
$this->Model_regist->insert($data);
$this->Model_regist->upload($gambar);
$this->index();
}
private function uploadImage(){
$config['upload_path'] = './gambar/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = true;
$nama_file = "gambar_".time();
$config['file_name'] = $nama_file;
$this->load->library('upload', $config);
if( ! $this->upload->do_upload('berkas')){
$error = array('error' => $this->upload->display_errors());
}else{
$this->upload->data();
}
$this->Model_regist->upload($gambar);
$this->Model_regist->insert($data);
$this->index();
}
I expect the photo that has been chosen can be saved in the database and can be saved in the file that I have made before. The file name is gambar.

Codeigniter Upload featured image and multiple images at the same time

I have modified the code to use a foreach loop, now I can access the files names, and the print_r is printing the files names in an array like this:
Array ( [0] => Uconn.png [1] => UW_Medicine.png [2] => Yale.png ) Axis.png
but I am still getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
and a databse error:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `yacht_slider` (`yacht_slide_id`, `slide`) VALUES (87, Array)
I just don't know how should I pass the looped files inside the model to upload.
I am trying to upload a featured image in one input, and multiple images in another input at the same time, I tried many ways and methods to do this, but I always end with an error, mostly Array to string conversion error.
the featured image is stored in one database table and the multiple images are stored in another table.
My current code:
HTML:
<div class="form-group">
<label for="" class="control-label">Image</label>
<input type="file" class="form-control" name="featured">
</div>
<div class="form-group">
<label for="" class="control-label">Slider</label>
<input type="file" class="form-control" name="userfile[]" multiple>
</div>
Model:
public function create_yacht($yacht_img, $slider){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'img' => $yacht_img,
'city' => $this->input->post('city'),
'category' => $this->input->post('category'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description')
);
$this->db->insert('yachts', $data);
$insert_id = $this->db->insert_id();
$data_4 = array(
'yacht_slide_id' => $insert_id,
'slide' => $slider
);
$this->db->insert('yacht_slider', $data_4);
}
Controller:
public function create_yacht(){
$data['title'] = 'Create';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/admin_header');
$this->load->view('admin/create_yacht', $data);
$this->load->view('templates/admin_footer');
}else{
$config['upload_path'] = './assets/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
$featured = 'featured';
if (!$this->upload->do_upload($featured)) {
$errors = array('errors' => $this->upload->display_errors());
$yacht_img = 'https://via.placeholder.com/1920x1080';
}else{
$data = array('upload_data' => $this->upload->data());
$yacht_img = $_FILES['featured']['name'];
}
foreach ($_FILES['userfile']['name'] as $name) {
$this->upload->initialize($config);
$this->upload->do_upload($name);
$data = array('upload_data' => $this->upload->data());
$slider = $_FILES['userfile']['name'];
}
print_r($slider);
print_r($yacht_img);
$this->yacht_model->create_yacht($yacht_img, $slider);
// redirect('admin');
}
}
I've cleaned up your code a bit and put in some error reporting so users won't be befuddled when an error occurs.
Controller:
public function create_yacht() {
$data['title'] = 'Create';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/admin_header');
$this->load->view('admin/create_yacht', $data);
$this->load->view('templates/admin_footer');
} else {
$this->load->library('upload');
$upload_path = './testupload/';
// just in case, make path if it doesn't exist
// if we can't die
if (!is_dir($upload_path) && #mkdir($upload_path, DIR_WRITE_MODE) == false) {
show_error('Could not make path!');
}
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['file_ext_tolower'] = true;
//$config['encrypt_name'] = true; // might be a good idea
$this->upload->initialize($config);
// featured image
if (!$this->upload->do_upload('featured')) {
show_error($this->upload->display_errors());
}
$yacht_img = $this->upload->data('file_name');
// multi images
$slider_images = array();
$multi_files = $_FILES['userfile'];
if (!empty($multi_files['name'][0])) { // if slider images are required remove this if
$multi_count = count($_FILES['userfile']['name']);
for ($i = 0; $i < $multi_count; $i++) {
$_FILES['userfile']['name'] = $multi_files['name'][$i];
$_FILES['userfile']['type'] = $multi_files['type'][$i];
$_FILES['userfile']['tmp_name'] = $multi_files['tmp_name'][$i];
$_FILES['userfile']['error'] = $multi_files['error'][$i];
$_FILES['userfile']['size'] = $multi_files['size'][$i];
if (!$this->upload->do_upload()) {
// failure cleanup to prevent orphans
#unlink($upload_path . $yacht_img);
if (count($slider_images) > 0) {
foreach ($slider_images as $image) {
#unlink($upload_path . $image);
}
}
show_error($this->upload->display_errors());
}
$slider_images[] = $this->upload->data('file_name');
}
}
$this->yacht_model->create_yacht($yacht_img, $slider_images);
}
}
Model:
public function create_yacht($yacht_img, $slider_images) {
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'img' => $yacht_img,
'city' => $this->input->post('city'),
'category' => $this->input->post('category'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description')
);
$this->db->insert('yachts', $data);
$insert_id = $this->db->insert_id();
if (count($slider_images) > 0) {
foreach ($slider_images as $image) {
$this->db->insert('yacht_slider', array('yacht_slide_id' => $insert_id, 'slide' => $image));
}
}
}
if you are using mySQL 7 then it supports json datatype and you can save as array. If your using lower version of MySQL best solution is to convert your image array into JSON format then insert that into table.
For more details on JSON in MySQL you can refer
https://dev.mysql.com/doc/refman/8.0/en/json.html#json-values

codeigniter upload image

Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,
this is my model file:
function uploadImg()
{
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000,
'encrypt_name' => true
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 200,
'height' => 200,
'encrypt_name' => true,
'max_size' => 2000
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
# Ret profil billed navn #
$file_array = $this->upload->data('file_name');
return $billed_sti['billed_sti'] = $file_array['file_name'];
//$this->db->where('username', $this->input->post('username'));
//$this->db->update('users', $profilBilledNavn);
}
This is my controller:
function opret() {
$this->form_validation->set_rules('overskrift', 'overskrift', 'required');
$this->form_validation->set_rules('description', 'description', 'required');
$this->form_validation->set_rules('indhold', 'indhold', 'required');
if($this->form_validation->run() == true)
{
$this->load->model('admin/nyheder_model');
$billed_sti = $this->nyheder_model->uploadImg();
$data = array(
'overskrift' => $this->input->post('overskrift'),
'description' => $this->input->post('description'),
'indhold' => $this->input->post('indhold'),
'billed_sti' => $billed_sti,
'brugernavn' => $this->session->userdata('username'),
'godkendt' => 'ja'
);
$this->db->insert('nyheder', $data);
redirect('admin/nyheder/index');
} else {
$this->index();
}
}
I do the image processing in the controller rather than the model.
"Models are PHP classes that are designed to work with information in your database."
from: http://codeigniter.com/user_guide/general/models.html
What you need to do is move the code for uploading the image to the controler.
function do_upload()
{
$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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Once you did that,
You can insert the name of the file from the $data variable created in this line:
$data = array('upload_data' => $this->upload->data());
and you can get the value like this:
$data['file_name']
The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.
I hope it helps.
Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below
//Controller Class
function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit
$this->upload->initalize($config);
if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement
Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.
Thanks
J

Categories