Solved

Error in form (CakePhp)

Posted on 2010-09-02
10
454 Views
Last Modified: 2013-12-13
Hi all,
I'm my view I am getting the following error:
Fatal error: Cannot use string offset as an array in ...

After adding this code:
echo $form->hidden('user_id', array('value'=>'37'));

The error is pointing to this code:
if($file['file']['error'] != 0) continue;

Just in case it helps ... here is the controller and view code:

/*****************************************/
/* Controller
/*****************************************/
<?php
class UploadsController extends AppController {
      
      var $name = 'Uploads';
      var $useTable = 'competitions_entry';
      var $components = array('RequestHandler','FileUpload');
      var $helpers = array('Html', 'Form', 'Javascript');
      
            
      function index() {
            $this->set('uploads', $this->Upload->find('all'));
            if($this->RequestHandler->isPost()) {
            $this->Upload->saveAll($this->data['Upload'], array('validate'=>'only')); // validation
            

            foreach($this->data['Upload'] as $number => $file) {
            if($file['file']['error'] != 0) continue; //do not save when PHP errors occur

            $this->Upload->create();
            if($this->Upload->save($file['file'],array('validate'=>false))) { // no need to validate now
            
            
            if(!$this->FileUpload->upload($file['file'])) {
            $this->Upload->delete($this->Upload->id); // remove the DB entry because there were upload errors
            }
            }
            }
            }
            }
            
} //end controller
?>


/**********************************/
/* The View
/********************************/
<?php
echo $form->create('Upload',array('type'=>'file', 'url' => 'uploads'));
echo $form->input('0.file',array('type'=>'file','label'=>'Image Upload: '));
echo $form->hidden('user_id', array('value'=>'37'));
echo $form->end('Submit');
?>

Hope you can help

Thanks





0
Comment
Question by:error77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 2

Assisted Solution

by:ciprian-dobrea
ciprian-dobrea earned 250 total points
ID: 33584621
print_d($file);die();

right before the line

if($file['file']['error'] != 0) continue; //do not save when PHP errors occur

that will show you the actual array structure of $file
0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 250 total points
ID: 33584666
Change that line to:

echo $form->hidden('user_id');

I think what you should be doing is, before render in the controller, you should do:

Add this to your controller in the index() action:

$this->data['Upload']['user_id'] = 37

0
 

Author Comment

by:error77
ID: 33584872
Not errors now but not saving to the table.

This is my controller now:

/*********************************/
/*  Controller
/********************************/
<?php
class UploadsController extends AppController {
      
      var $name = 'Uploads';
      var $useTable = 'competitions_entry';
      var $components = array('RequestHandler','FileUpload');
      var $helpers = array('Html', 'Form', 'Javascript');
      
            
      function index() {
            $this->set('uploads', $this->Upload->find('all'));
            
            if($this->RequestHandler->isPost()) {
            $this->data['Upload']['user_id'] = 37; //new line
            $this->Upload->saveAll($this->data['Upload'], array('validate'=>'only')); // validation
            
                foreach($this->data['Upload'] as $number => $file) {
            
            if($file['file']['error'] != 0) continue; //do not save when PHP errors occur

            $this->Upload->create();
            if($this->Upload->save($file['file'],array('validate'=>false))) { // no need to validate now
            
            
            if(!$this->FileUpload->upload($file['file'])) {
            $this->Upload->delete($this->Upload->id); // remove the DB entry because there were upload errors
            }
            }
            }
            }
            }
      
}
?>

and my view

/********************************************/
/*  View
/*******************************************/
<?php
echo $form->create('Upload',array('type'=>'file', 'url' => 'uploads'));
echo $form->input('0.file',array('type'=>'file','label'=>'Image Upload: '));
echo $form->hidden('user_id'); //Changed line
echo $form->end('Submit');
?>
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:error77
ID: 33587897
Any ideas please?
0
 

Author Comment

by:error77
ID: 33591985
Anyone please?
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 33592205
What exactly is the problem?
0
 

Author Comment

by:error77
ID: 33592487
The problem is that I have a file upload fuctionality which works fine and saves to the table. But when I upload the file I need to also add a couple of more fields to it, for example, the user_id.
I have tried  your last code as the only issue is that it's not saving the user_id value to the table. It's only inserting the image

Here is all the code:

/***********************************/
/* The Component
/*************************************/
<?php
class FileUploadComponent extends Object {
      var $components = array('RequestHandler');
      function upload($data) {
      $destination = 'img/uploads/'.$data['name']; // Create uploads folder and set proper permissions!
      if(move_uploaded_file($data['tmp_name'], $destination)) {
      return true;
      }
      return false;
      }
}
?>

/**********************************************/
/* The Model
/**********************************************/
<?php
class Upload extends AppModel {
var $name = 'Upload';
var $useTable = 'competitions_entry';
var $validate = array(
'file' => array(
'rule' => array('validateFile', false),
'message' => 'There is a problem with uploading the file.' //message
)
);
function validateFile($data, $required = false) {
$file = array_shift($data); // for easier handling
if ($required && $file['size'] == 0) return false; // If no file is uploaded
if ($file['error'] !== 0) return false;
return is_uploaded_file($file['tmp_name']); // If everything is fine
}
}
?>

/*****************************************/
/* Controller
/*****************************************/
<?php
class UploadsController extends AppController {
     
      var $name = 'Uploads';
      var $useTable = 'competitions_entry';
      var $components = array('RequestHandler','FileUpload');
      var $helpers = array('Html', 'Form', 'Javascript');
     
           
      function index() {
            $this->set('uploads', $this->Upload->find('all'));
            if($this->RequestHandler->isPost()) {
              $this->data['Upload']['user_id'] = 37; //new line ... not sure if in right place
            $this->Upload->saveAll($this->data['Upload'], array('validate'=>'only')); // validation
           

            foreach($this->data['Upload'] as $number => $file) {
            if($file['file']['error'] != 0) continue; //do not save when PHP errors occur

            $this->Upload->create();
            if($this->Upload->save($file['file'],array('validate'=>false))) { // no need to validate now
           
           
            if(!$this->FileUpload->upload($file['file'])) {
            $this->Upload->delete($this->Upload->id); // remove the DB entry because there were upload errors
            }
            }
            }
            }
            }
           
} //end controller
?>


/**********************************/
/* The View
/********************************/
<?php
echo $form->create('Upload',array('type'=>'file', 'url' => 'uploads'));
echo $form->input('0.file',array('type'=>'file','label'=>'Image Upload: '));
echo $form->hidden('user_id'); //NEW ... Not Saving to table
echo $form->end('Submit');
?>

This is all the code.
Hope you can help

thanks

0
 
LVL 11

Expert Comment

by:mattibutt
ID: 33594589
Echo user id maybe its not meeting data type of mysql or its unable to post
0
 

Author Comment

by:error77
ID: 33595050
Tried a var_dump on the view on data and got back null. That's after adding       $this->data['Upload']['user_id'] = 37; to the controller
0
 

Author Comment

by:error77
ID: 33595134
$this->data is returning NULL
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question