Solved

Error in form (CakePhp)

Posted on 2010-09-02
10
453 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
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use Select Query to Return Results as a Form 9 45
PHP 5.6 and 7.x 4 41
Multi line FPDF footer: 3 30
Convert php array to comma seperated list 19 36
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

696 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