CodeIgniter Suddenly Cannot Upload Image and says Cannot Be Null - codeigniter-3

Greeting everyone and especially to Senior of CodeIgniter Developer.
I have a problem with website that was built from CodeIgniter (I am not the developer inherited by previous epmployer). This website cannot working properly, especially when upload the image and shows warning about
Error Number: 1048 Column 'article_image' cannot be null
I did try with find the problem on the script and database, but nothing change and because no one change the codes & contents. Today, i try again with changing the "Null into yes" (previously is "No"), and tried to upload the article. It is miracle that it is working but the next problem is the image is gone (broken). I search at other and looking people with same problem with me says that i need to upgrade the CodeIgniter. Mine is 3.0.0 while the latest is 3.1.10. I copy paste the content inside /System and /views/error/cli not making better but make it worse, the image at the web page is gone(missing).
This is my Article_model
defined('BASEPATH') OR exit('No direct script access allowed');
class Article_model extends CI_Model {
public function get_all()
{
// $this->db->order_by('article_date', 'desc');
// $this->db->order_by('article_view', 'desc');
// $query = $this->db->get_where('article', array('flag !=' => 3));
// return $query->result_array();
$query = $this->db->query("SELECT A.*,B.*, A.id AS id_article FROM article AS A JOIN category B ON A.article_category = B.id WHERE A.flag != 3 ORDER BY article_date DESC, article_view DESC ");
return $query->result_array();
}
public function check($article_id)
{
$query = $this->db->get_where('article', array('flag !=' => 3, 'id' => $article_id));
return $query->row_array();
}
public function get_category()
{
$query = $this->db->order_by('category_name', 'ASC')->get_where('category', array('flag' => 1));
return $query->result_array();
}
public function get_tag()
{
$query = $this->db->order_by('tag_name', 'ASC')->get_where('tag', array('flag' => 1));
return $query->result_array();
}
public function get_selected_tag($article_id)
{
$query = $this->db
->select('tag.id, tag_name')
->join('article_tag', 'tag_id = tag.id')
->where(array('tag.flag' => 1, 'article_id' => $article_id))
->get('tag');
return $query->result_array();
}
public function insert()
{
$this->load->helper('upload');
$this->load->library('image_moo');
$image = file_upload('article_image', 'upload/article');
$this->image_moo->load($image['full_path']);
$this->image_moo->resize(924, 527)->save($image['path'] . '/' . $image['file_name'], TRUE);
$this->image_moo->resize_crop(100, 69)->save($image['path'] . '/thumb/' . $image['file_name']);
$this->image_moo->resize_crop(367, 232)->save($image['path'] . '/cover/' . $image['file_name']);
$insert_data = array(
'article_author' => $this->session->admin_id,
'article_title' => $this->input->post('article_title'),
'article_slug' => url_title($this->input->post('article_title'), '-', TRUE),
'article_category' => $this->input->post('article_category'),
'article_image' => $image['file_name'],
'article_content' => $this->input->post('article_content'),
'is_popup' => $this->input->post('is_poup'),
'article_date' => $this->input->post('article_date'),
);
$this->db->insert('article', $insert_data);
$article_id = $this->db->insert_id();
foreach ($this->input->post('article_tag') as $tag)
{
// Check apakah tag itu udah ada di database?
if (is_numeric($tag))
{
$tag_id = $tag;
}
else
{
$this->db->insert('tag', array('tag_name' => strtolower(trim($tag)), 'tag_slug' => url_title(trim($tag), '-', TRUE)));
$tag_id = $this->db->insert_id();
}
if ( ! $this->db->get_where('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id))->row_array())
{
$this->db->insert('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id));
}
}
}
public function update($id)
{
$this->load->helper('upload');
$this->load->library('image_moo');
$image = file_upload('article_image', 'upload/article');
$this->image_moo->load($image['full_path']);
$this->image_moo->resize(924, 527)->save($image['path'] . '/' . $image['file_name'], TRUE);
$this->image_moo->resize_crop(100, 69)->save($image['path'] . '/thumb/' . $image['file_name']);
$this->image_moo->resize_crop(367, 232)->save($image['path'] . '/cover/' . $image['file_name']);
$insert_data = array(
'article_author' => $this->session->admin_id,
'article_title' => $this->input->post('article_title'),
'article_slug' => url_title($this->input->post('article_title'), '-', TRUE),
'article_category' => $this->input->post('article_category'),
'is_popup' => $this->input->post('is_popup'),
'article_content' => $this->input->post('article_content'),
'article_date' => $this->input->post('article_date'),
);
if ($image)
{
$insert_data['article_image'] = $image['file_name'];
}
$article_id = $id;
$this->db->where('id', $id);
$this->db->update('article', $insert_data);
$this->db->where('article_id', $id);
$this->db->delete('article_tag');
foreach ($this->input->post('article_tag') as $tag)
{
// Check apakah tag itu udah ada di database?
if (is_numeric($tag))
{
$tag_id = $tag;
}
else
{
$this->db->insert('tag', array('tag_name' => strtolower(trim($tag)), 'tag_slug' => url_title(trim($tag), '-', TRUE)));
$tag_id = $this->db->insert_id();
}
if ( ! $this->db->get_where('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id))->row_array())
{
$this->db->insert('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id));
}
}
}
}
What should i do guys? i did backup but it remain same like after upgraded. Thank you. Web Url (http://www.hidupseimbangku.com/)

First. If you want to upgrade CI Version you must follow Upgrading Guide one by one. There may be break changes and you need to correct them, you can find this guide here:
https://www.codeigniter.com/userguide3/installation/upgrading.html
I suggest you rollback upgrade changes and start again doing one by one. It is not hard, it is just time consuming.
The error you're getting is probably related to an error of uploading process. What is file_upload?
I also suggest you read this, for properly file upload.

Related

Rest API : #1062 - duplicate entry for key 'primary'

I want upload some image to server with rest API using Codeigniter.
But I have a problem after upload image / refresh phpmyadmin on this table.
I know this problem is classic, what I'm already todo:
Re-Create the table 2 times.
Make ID to Autoincrement (actually I'm make it AI at first make table)
Not include ID on insert operation, and make it AI automatic.
I'm already truncate the table 2 times
but nothing change anything, at I'm refresh page phpmyadmin this error always appear.
What's my mistake ?
Thanks
If you think my error because my code Rest-API, here's my code:
Controller:
public function upload_post()
{
// $config['max_size'] = '100';
// $config['max_width'] = '1024';
// $config['max_height'] = '768';
$msgCreated = ['status' => true, 'message' => 'Image Has Been Upload'];
$msgFailCreated = ['status' => false, 'message' => 'Failed to Upload Image !'];
$config['upload_path'] = './image/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file')) {
$this->set_response($msgFailCreated, 404);
} else {
$info = $this->upload->data();
$image_path = base_url("image/" . $info['raw_name'] . $info['file_ext']);
$data = [
"name_image" => $this->input->post('name_image'),
"image" => $image_path
];
if ($this->mahasiswa->insertImage($data)) {
$this->set_response($msgCreated, 201);
} else {
$this->set_response($msgFailCreated, 400);
}
}
}
Model:
//? Membuat Upload Image
public function insertImage($data){
return $this->db->insert('image', $data);
}

Codeigniter - Issue while uploading png image

I have implemented a web service for update profile image using Codeigniter framework. But I have an issue while uploading png image. It always gives an error saying -
The filetype you are attempting to upload is not allowed.
Following is my config array:
$config = array(
'upload_path' => BASEPATH .'../assets/uploads/profile_images',
'allowed_types' => 'jpg|png|jpeg',
'overwrite' => TRUE,
'max_size' => "2048000",
);
I have added "png" in allowed types, still it is showing error.
Please guide.
Instead of using
config['allowed_types'] = 'jpg|png|jpeg'
I changed my config array to
$config = array(
'upload_path' => BASEPATH .'../assets/uploads/profile_images',
'allowed_types' => '*',
'overwrite' => TRUE,
'max_size' => "2048000",
);
And added custom form validation rule
$this->form_validation->set_rules('profile_image', '', 'callback_file_check');
and its corresponding function named file_check(). Here I will be checking file type and will return an error if it is not an image.
/**
Function: file_check
Description: file value and type check during validation
Date: 6th July 2018
**/
public function file_check($str)
{
$allowed_mime_type_arr = array('image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
$mime = mime_content_type($_FILES['profile_image']['tmp_name']);
if(isset($_FILES['profile_image']['tmp_name']) && $_FILES['profile_image']['tmp_name']!="")
{
if(in_array($mime, $allowed_mime_type_arr))
{
return true;
} else {
$this->form_validation->set_message('file_check', 'Please select only jpg/jpeg/png file.');
return false;
}
}else{
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return false;
}
}
So my upload function will look like this
if($this->form_validation->run() == true) {
if (!$this->upload->do_upload('profile_image')) {
return $this->response(['msg'=>$this->upload->display_errors(),'success'=>false,'code'=>400]);
} else {
$fileName = $this->upload->data();
$name = $fileName['file_name'];
}
//Save into database
$image = $this->Api_model->update_profile_image('assets/uploads/profile_images/'.$name, $request_data['user_id']);
if($image)
return $this->response(['msg'=>'Image updated successfully','success'=>true,'code'=>200, 'image_path' => base_url().$image]);
else
return $this->response(['msg'=>'Image updated failed','success'=>false,'code'=>400]);
} else {
return $this->response(['msg' => validation_errors(), 'success' => false, 'code' => 401]);
}

Moodle Error writing to database

I am getting Error writing to database in moodle:
Code:
if (is_siteadmin()) {
class addschedule_form extends moodleform {
function definition () {
$mform =& $this->_form;
$mform->addElement('text', 'id', 'Enter ID:');
$mform->setType('id', PARAM_TEXT);
$this->add_action_buttons(true, 'submit');
}
}
$ti_form = new addschedule_form();
$ti_form->get_data();
if ($ti_form->is_cancelled()) {redirect('index.php');}
if ($recs = $ti_form->get_data()) {
$deleteit = $DB -> delete_records('DELETE * FROM `mdl_schedules` WHERE id = ' . $recs -> id . '');
redirect('schedule.php');
}
}
else { }
$ti_form->display();
What could be the reason? Any reference or help will be much appreciated.
Regards
The syntax is
$DB->delete_records('schedules', array('id' => $recs->id));
You might want to keep this open in a tab for reference - https://docs.moodle.org/dev/Data_manipulation_API

How do I populate a Zend Form from a Doctrine Model with Many To One relationships?

I have an entity setup called Lead which contains a car make, model, etc and this Lead is mapped by a Many to One relationship to a Client entity, which has forname, surname, etc.
i.e. A client may have many leads
I have created a toArray function which gets the data from the Lead,
public function toArray()
{
$record_data = get_object_vars($this);
$formatted_record_data = array();
foreach($record_data as $name=>$value){
if (is_object($value)){
if (get_class($value) == 'DateTime') {
$value = $this->datetimeToString($value);
} else {
$value = $value->toArray();
}
}
$formatted_record_data[$this->from_camel_case($name)] = $value;
}
return $formatted_record_data;
}
which then populates a Zend Form using:
$record = $this->_em->getRepository($this->_entity)->find($this->_primaryId);
$form->setDefaults($record->toArray());
This works fine for the fields for the Lead entity which are populated, but it does not populate the Client-based fields e.g. forname.
EDIT
I have fixed my problem by the following method:
1) Adding the following method to my Update Action.
$this->_record = $this->_em->getRepository($this->_entity)->find($this->_primaryId);
$this->_form->setRecord($this->_record);
$this->view->form = $this->_form;
2) Adding the following method to my Form Model
public function setRecord($record)
{
$data = array('registration' => $record->registration,
'make' => $record->make,
'model' => $record->model,
'pav' => $record->pav,
'salvage_value' => $record->salvageValue,
'forname' => $record->client->forname,
'surname' => $record->client->surname,
'vehicle_address1' => $record->vehicleAddress1,
'vehicle_address2' => $record->vehicleAddress2,
'vehicle_address3' => $record->vehicleAddress3,
'vehicle_address4' => $record->vehicleAddress4,
'vehicle_address5' => $record->vehicleAddress5,
'vehicle_postcode' => $record->vehiclePostcode,
'category' => $record->category,
'colour' => $record->colour
);
$this->setDefaults($data);
}
This way I can manually get the related data, in this case:
'forname' => $record->client->forname,
'surname' => $record->client->surname,
and add them to the form using:
$this->setDefaults($data);
try :
$leads = $record->toArray();
$client_info = array('name' => 'test', 'surname' => 'test 2'); // or if from db use what you did for the leads.
$defaults = array_merge($leads, $client_info);
$form->setDefaults($defaults);

How to create sub category on Magento APi

I am currently using Magento ver. 1.5.0.1. Can anyone tell me how do i create a sub category using soapv2.
Here my code format
category_data = { "name" => "LIGHTING", "is_active" => 1 }
soap.call('catalogCategoryCreate',session,4,category_data,1, "include_in_menu")
I got some errors when i run this code.
: Attribute "include_in_menu" is required. (SOAP::FaultError)
Is that any solution for this problem.
Thanks.
category_data = { "name" => "LIGHTING", "is_active" => 1, "include_in_menu" => 1 }
This is how you can create category using SOAP V2
<?php
$host = "192.168.0.10/~aliasgar/magentoext/index.php"; //our online shop url
$client = new SoapClient("http://".$host."/api/v2_soap/?wsdl"); //soap handle
$apiuser= "aliasgar"; //webservice user login
$apikey = "aliasgar"; //webservice user pass
try {
$sess_id= $client->login($apiuser, $apikey); //we do login
$result = $client->catalogCategoryCreate($sess_id, 3, array(
'name' => 'Test Category',
'is_active' => 1,
'available_sort_by' => array('position'),
'default_sort_by' => 'position',
'include_in_menu' => '1',
));
var_dump ($result);
}
catch (Exception $e) { //while an error has occured
echo "==> Error: ".$e->getMessage(); //we print this
exit();
}
?>
Here 3 in the catalogCategoryCreate function is the parent category id.