I'm try to update data in oracle table from excel file (.xlsx) use CodeIgniter. I've already uploaded the excel file but when I try to update the data I get the following error message:
Message: Array to string conversion
Filename: database/DB_driver.php
Line Number: 1524
Backtrace:
File: C:\xampp\htdocs\web_excel_ci_test\application\models\RoadmapModel.php
Line: 84
Function: update
File: C:\xampp\htdocs\web_excel_ci_test\application\controllers\Roadmap.php
Line: 209
Function: update_data
Controller:
function update(){
$this->load->library('session');
$fileName = $this->session->flashdata('fileName');
$fileName2 = $this->session->flashdata('fileName2');
include APPPATH.'third_party/PHPExcel/PHPExcel.php';
$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$fileName);
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = [];
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
array_push($data, [
'YEAR'=>$row['A'],
'PROVINCEID'=>$row['B'],
'PROVINCE'=>$row['C'],
'PLAN_A'=>$row['D'],
'ACTUAL_A'=>$row['E'],
]);
}
$numrow++;
}
$year = $this->input->post('YEAR');
$this->RoadmapModel->update_data($year, $fileName2, $data);
redirect("Roadmap");
}
Model:
function update_data($year, $fileName2, $data){
for ($i=0; $i < count($year) ; $i++) {
$this->db->where('YEAR', $year[$i]);
$this->db->update($fileName2, $data);
}
}
i think your $data is a multidimentional array, try to use
$this->db->update_batch
to process batch update. You can also look here to for more options.
Related
I am trying to import data via excel sheet into database with codeigniter application.I am using phpexcel. However the code is right but i am getting an error which states:
Error Number: 1054
Unknown column 'joker' in 'field list'
INSERT INTO studentsaccount (joker) VALUES ('')
Filename: C:/xampp/htdocs/Nalanda_Library/system/database/DB_driver.php
Line Number: 691
however my code is as follows: for controller
public function studentaccountimport(){
$this->load->model('Department');
$file = $_FILES['upload']['tmp_name'];
//load the excel library
$this->load->library('excel');
//read file from path
$objPHPExcel = PHPExcel_IOFactory::load($file);
//get only the Cell Collection
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
//extract to a PHP readable array format
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only.
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
$this->Department->modeluploadation($data_value);
}
}
}
for model:
public function modeluploadation($data){
$this->db->insert('studentsaccount',$data);
}
i am novice in codeigniter here so please help
You need to specify column names in insert query..
try
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
$data['header'] = $header;
$data['values'] = $arr_data;
$this->Department->modeluploadation($data);
Ok the reason for your problem is you need to pass an array of key=>value pairs as the parameter for insert().
I'm not sure why Safins answer has been marked down because he is right. So when you should set modeluploadation as:
public function modeluploadation($data){
$this->db->insert('studentsaccount',array('field_name'=>$data_value));
}
This code throw Exception:
public function actionSetdubl() {
$dubls = Yii::$app->request->post('dubl');
$parent = Yii::$app->request->post('parent');
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->all()[0];
for ($i = 0; $i < sizeof($dubls); ++$i) {
$val = $dubls[$i];
$jur = JurForm::findOne($val);
$jur->CFLDUBL = 'Yes';
$jur->DUBLMDM_ID = $parentInfo['MDM_ID'];
$jur->DCHANGEDATE = date('Y-m-d H:i:s');
$jur->save();
}
return Yii::$app->getResponse()->redirect('/index.php?r=jur/analysis');
}
on the line with code $jur = JurForm::findOne($val);.
Exception:
Setting unknown property: app\models\JurForm::PKJUR.
DB: Oracle.
ActiveRecord2 has a hard time automatically mapping table names that start with a capital letter.
So for these columns you have to go into your model class and formally declare them:
public $PKJUR;
maybe better?
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->one()
also i think PKJUR is not defined in DB.
we are using PostGreSql database, when we run following code unit, it shows error of.
Severity: Warning
Message: Illegal string offset 'server'
Filename: postgre/postgre_driver.php
Message: Cannot modify header information - headers already sent by (output started at system/core/Exceptions.php:185)
Model Code:
public function tracks_add( $id ) {
$cnt = 0;
$date = date('Y-m-d H:i:s');
$s_title = $this->input->post('s_title');
$s_singer = $this->input->post('s_singer');
$s_url = $this->input->post('s_url');
foreach ($s_title as $s_title) {
$this->db->set( array('a_id'=> $id, 's_title' => $s_title, 's_singer' => $s_singer[$cnt], 's_url' => $s_url[$cnt], 'date'=> $date) );
$this->db->insert('soundtracks');
$cnt++;
}
}
You can find this issue here :
Postgres db driver insert issue
Change line 331 to
$v = pg_version($this->conn_id); $v = isset($v['server']) ? $v['server'] : 0; // 'server' key
How to use jQuery-File-Upload with PHP and database?
I want to insert or delete rows about images when I upload or delete images and that name of each image will be as time() when they are uploaded to the directory.
All result I found through google tell me that I need edit to upload.class.php but the last release has index.php and UploadHandler.php only...
file UploadHandler.php has class UploadHandler with code
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$_SERVER['HTTP_CONTENT_DISPOSITION']
)) : null;
$file_type = isset($_SERVER['HTTP_CONTENT_DESCRIPTION']) ?
$_SERVER['HTTP_CONTENT_DESCRIPTION'] : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ?
preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null;
$size = $content_range ? $content_range[3] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) {
$info[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
$file_name ? $file_name : $upload['name'][$index],
$size ? $size : $upload['size'][$index],
$file_type ? $file_type : $upload['type'][$index],
$upload['error'][$index],
$index,
$content_range
);
}
} else {
// param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array:
$info[] = $this->handle_file_upload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
$file_name ? $file_name : (isset($upload['name']) ?
$upload['name'] : null),
$size ? $size : (isset($upload['size']) ?
$upload['size'] : $_SERVER['CONTENT_LENGTH']),
$file_type ? $file_type : (isset($upload['type']) ?
$upload['type'] : $_SERVER['CONTENT_TYPE']),
isset($upload['error']) ? $upload['error'] : null,
null,
$content_range
);
}
return $this->generate_response($info, $print_response);
}
public function delete($print_response = true) {
$file_name = $this->get_file_name_param();
$file_path = $this->get_upload_path($file_name);
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
if ($success) {
foreach($this->options['image_versions'] as $version => $options) {
if (!empty($version)) {
$file = $this->get_upload_path($file_name, $version);
if (is_file($file)) {
unlink($file);
}
}
}
}
return $this->generate_response($success, $print_response);
}
What rows do I need to add to insert or delete file names in mysql?
P.S.: I use PHP
I use docementation and now can said that instead file upload.class.php need edit file UploadHandler.php
and than use next:
Search this line - > $this->options = array(
Add the following Code in the next lines :
// mysql connection settings
'database' => '**YOUR DATABASE**',
'host' => '**localhost**',
'username' => '**YOUR USERNAME**',
'password' => '**YOUR PASSWORD**',
// end
So now you have to write a function for the SQL Query, copy & paste the following code for example after the handle_file_upload function :
function query($query) {
$database = $this->options['database'];
$host = $this->options['host'];
$username = $this->options['username'];
$password = $this->options['password'];
$link = mysql_connect($host,$username,$password);
if (!$link) {
die(mysql_error());
}
$db_selected = mysql_select_db($database);
if (!$db_selected) {
die(mysql_error());
}
$result = mysql_query($query);
mysql_close($link);
return $result;
}
Add file details to database
I explain this function with a picture upload, so here we save the picture name to the database Add this function also too the upload.class.php
function add_img($whichimg)
{
$add_to_db = $this->query("INSERT INTO yourtable (**yourcolumnone**) VALUES ('".$whichimg."')") or die(mysql_error());
return $add_to_db;
}
so in this function we call the function query with the string between the clamps.
You could also insert other details too, for example, the file size.
At least we have to call this function, with the following code at the end of the function handle_file_upload. Paste the following code underneath or over this line : $file->size = $file_size;
$file->upload_to_db = $this->add_img($file->name);
Delete the entry we created
Deleting the entry we made before is very easy, we create a new function which makes also an sql query to delete it.
function delete_img($delimg)
{
$delete_from_db = $this->query("DELETE FROM yourtable WHERE yourcolumnone = '$delimg'") or die(mysql_error());
return $delete_from_db;
}
Now we must call the function, this time of the delete function.
Go to the delete function and search this line : if ($success) { paste the following code over this.
$this->delete_img($file_name);
Enjoy=)
You should overload the default methods (as described in the documentation) in order to add your custom features.
If you modify original files, you'll have more work when a new version (or a fix) of the plugin is released.
For my own purposes, I basically defined a custom class which extends the original one and modified only the /server/php/index.php file:
class myUploadHandler extends UploadHandler {
//do your stuff
//for exemple if you are using a DB:
//overload methods like handle_file_upload() for insertion in a DB,
//set_additional_file_properties() if you need more fields linked to each uploaded file
// or delete() also if you want to remove from DB
}
$upload_handler = new myUploadHandler(array(
'user_dirs' => true,
'download_via_php' => true,
));
The following code uploads multiple images no problem. However, I'm trying to get it to update a field in a table based on what iteration the loop is in. PROBLEM: The IF Statement seems to not work when looped. I.e. it only adds the first file_name to the database.
Anyone see what I'm doing wrong here? Much appreciated if so!!!
for ($i = 1; $i < 4; $i++)
{
/* Handle the file upload */
$upload = $this->upload->do_upload('image' . $i);
/* File failed to upload - continue */
if ($upload === FALSE)
continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
if ($i == 1)
{
$filenames1 = array(
'product_image_front' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames1);
}
if ($i == 2)
{
$filenames2 = array(
'product_image_back' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames2);
}
if ($i == 3)
{
$filenames3 = array(
'product_image_back' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames3);
}
}
insert_id - Get the ID generated in the last query.
Store it in a variable before the loop.