Solved

Error in form (CakePhp)

Posted on 2010-09-02
10
448 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Any ideas please?
0
 

Author Comment

by:error77
Comment Utility
Anyone please?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
What exactly is the problem?
0
 

Author Comment

by:error77
Comment Utility
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
Comment Utility
Echo user id maybe its not meeting data type of mysql or its unable to post
0
 

Author Comment

by:error77
Comment Utility
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
Comment Utility
$this->data is returning NULL
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mysqli Query 5 40
Time difference 10 33
Why does this login script not work on a live server? 19 38
update field on focusout 15 19
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

762 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

9 Experts available now in Live!

Get 1:1 Help Now