doutou7961 2016-05-26 09:28
浏览 27

如何在yii 1.13中保存数据库中所选爱好的ID

My Registration PageI need to store the id of the selected hobbies from my check box in database. First, in my Hobbies table, i view all the hobby names and display it in check box. Now, whichever the hobbies selected, it stores the id of the hobbies in another table.

This is my code in views/sample/register.php

<div class="row">
    <?php echo $form->labelEx($model,'hobby');
    $opts = CHtml::listData(Hobbies::model()->findAll(),'hobby_id','hobby_name');
    echo $form->checkBoxList($model,'hobby',$opts, array('labelOptions'=>array('style'=>'display:inline'),'separator'=>'  '));
    echo $form->error($model,'hobby'); ?>
</div>

And this is my code in controllers/SampleController.php

if($model->validate())
        {
            $model->hobby = implode(",",$model->hobby);
            if($model->save())
            {
               $this->redirect(array('site/login'));
            }
            return;
        }

But it displays an error as PHP warning mb_strlen() expects parameter 1 to be string, array given. Please anybody help.

  • 写回答

1条回答 默认 最新

  • dse323222 2016-05-26 09:57
    关注

    Try this code in your controller

    $postArr = $_POST[<FORM NAME>]; //Post data to an array
    $model->hobby = implode(", ", $postArr['hobby']); //Getting details from post array
    if($model->validate())
        {
            if($model->save())
            {
               $this->redirect(array('site/login'));
            }
            return;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么