Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error in form (CakePhp)

Posted on 2010-09-02
10
Medium Priority
?
458 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 1000 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

926 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