Friday, December 6, 2013

Save Multiple records in database

Dear All,

We are facing some problems when we insert multiple  records in the database.

Following are the view.ctp file code


 <!-- main content box -->
      <div class="maincont-box fr">
        <div class="add-work-box">
          <h3>Security Questions And Answers</h3>
          <div class="add-work-formbox no-bdr">
      <?php echo $this->Form->create('UserQuestion');?>
     

         
<?php  $i=0;
foreach ($questions as $question):

 $id = $question['Question']['id'];
//echo $this->Form->input('id',array('type'=>'hidden','default'=>$id));





// echo $this->Form->input('user_id',array('type'=>'hidden','default'=>$users['UserDetail']['id']));
 echo $this->Form->input('UserQuestion'.$i.'user_id', array('type' => 'hidden','label' => '','name'=>"data[UserQuestion][$i][user_id]",'value'=>$users['UserDetail']['id']));
echo $this->Form->input('UserQuestion'.$i.'question_id', array('type' => 'hidden','label' => '','name'=>"data[UserQuestion][$i][question_id]",'value'=>$id));



?>
      <?php echo h($question['Question']['question']);?>
       
          <!-- <input type="text" name="question[]" class="text-input" /> -->
    
     
     
   <?php
   //echo $this->Form->input("answer".$id,array('placeholder' => 'Answer Name', 'label' => false, 'class' => 'top fl','default'=>''));
   echo $this->Form->input('UserQuestion'.$i.'answer', array('type' => 'text','label' => '','name'=>"data[UserQuestion][$i][answer]"));

     
      echo "<br>";
      $i++;
  endforeach;
  ?>            
           
         
            <div class="cl"></div>
  



            <div class="cl"></div>
            <p><a href="#" onClick='submitanswerForm();' class="btn">Save</a> <?php echo $this->Html->link("Cancel", array('controller'=>'users', 'action'=>'employer_edit'), array("class"=>"btn2", "escape"=>false)); ?></p>



        <?php echo $this->Form->end();?>
          </div>         
          <div class="cl"></div>
        </div>
     
      </div>
      <!-- main content box end -->

      <script language ="javascript" type = "text/javascript" >
    function submitanswerForm()
    {
    $(".loader-box").show();
       $("#UserQuestionQuestionsForm").submit();
    }
</script>


Following are the controlle code

if ($this->request->is('post')) {
       
                     
                 $this->UserQuestion->create();
                $this->UserQuestion->saveMany($this->request->data);

}

When we run the code below are the details in the array


\app\Controller\UsersController.php (line 932)
array(  	'UserQuestion' => array(  		(int) 0 => array(  			'user_id' => '100',  			'question_id' => '8',  			'answer' => 'df'  		),  		(int) 1 => array(  			'user_id' => '100',  			'question_id' => '3',  			'answer' => 'dfdf'  		),  		(int) 2 => array(  			'user_id' => '100',  			'question_id' => '9',  			'answer' => 'dfdf'  		)  	)  )  
  But the record is not inserted into the table and no errors is displayed.

Following are the table fields:

CREATE TABLE IF NOT EXISTS `user_questions` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) NOT NULL,
  `question_id` int(10) NOT NULL,
  `answer` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


Pl help me what are the things we miss.



--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: