Solved

Error in form (CakePhp)

Posted on 2010-09-02
10
449 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
 

Author Comment

by:error77
ID: 33587897
Any ideas please?
0
 

Author Comment

by:error77
ID: 33591985
Anyone please?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change password links 7 74
Php Email function not working with Template 8 38
what is best version of php to use 6 46
Mysql sync between 3-4 mysql db 4 25
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now