PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How can I save this kind of data in cakePHP 2.3?

 April 22, 2022     cakephp, cakephp-2.3, html, php     No comments   

Issue

First of all am using cakePHP 2.3 framework and I have created form where I also have some relationships. The good thing is that I'm able to save the data, if the fields are not in array form like below.

<input type="text" name="data['Academicrecord']['school'][]" />
<input type="text" name="data['Academicrecord']['award'][]" />
<input type="text" name="data['Academicrecord']['from'][]" />
<input type="text" name="data['Academicrecord']['to'][]" />

But I get this error when I submit the form data above.

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

Look at the info generated by the cakePHP in built debug function.

debug($this->request->data);

'Academicrecord' => array(
    'school' => array(
        (int) 0 => 'Kansanga Primary School',
        (int) 1 => 'Tropical High School'
    ),
    'award' => array(
        (int) 0 => 'P.L.E',
        (int) 1 => 'U.C.E'
    ),
    'from' => array(
        (int) 0 => '1997',
        (int) 1 => '2003'
    ),
    'to' => array(
        (int) 0 => '2002',
        (int) 1 => '2006'
    )
)

On my way to solving this problem, I thought I could use the for loop to iterate the submitted items and then send them(data) to the model. Unfortunately my for loops cannot iterate the until the second row.

          echo  "The counts: ".count( $this->request->data['Academicrecord']['school']).'<br/>';

       for($i=0;$i<count( $this->request->data['Academicrecord']['school']);$i++){

         $this->request->data['Academicrecord']['staff_id'] = $this->Staff->id;
         echo "{$i} :".$this->request->data['Academicrecord']['school'] =   $this->request->data['Academicrecord']['school'][$i];
         echo ' '.$this->request->data['Academicrecord']['award']  =   $this->request->data['Academicrecord']['award'][$i];
         echo ' '.$this->request->data['Academicrecord']['from']   =   $this->request->data['Academicrecord']['from'][$i];
         echo ' '.$this->request->data['Academicrecord']['to']     =   $this->request->data['Academicrecord']['to'][$i];
         echo "<br/>";

           //$this->Staff->Academicrecord->save($this->request->data);
       }

Sample result after submitting.

The counts of rows: 2

Row 0 :Kansanga Primary School PLE 1990 2004


Solution

In loop you need to create the data like:

echo  "The counts: ".count( $this->request->data['Academicrecord']['school']).'<br/>';

       for($i=0;$i<count( $this->request->data['Academicrecord']['school']);$i++){

         $this->request->data['Academicrecord']['staff_id'] = $this->Staff->id;
         echo "{$i} :".$this->request->data['Academicrecord']['school'] =   $this->request->data['Academicrecord']['school'][$i];
         echo ' '.$this->request->data['Academicrecord']['award']  =   $this->request->data['Academicrecord']['award'][$i];
         echo ' '.$this->request->data['Academicrecord']['from']   =   $this->request->data['Academicrecord']['from'][$i];
         echo ' '.$this->request->data['Academicrecord']['to']     =   $this->request->data['Academicrecord']['to'][$i];
         //echo "<br/>";
         **$this->Staff->Academicrecord->create();**

         $this->Staff->Academicrecord->save($this->request->data);
       }

And i am not sure why you are using $this->Staff->Academicrecord instead of $this->Academicrecord.



Answered By - Vikram
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing