【问题标题】:Save multiple same named field to different records将多个同名字段保存到不同的记录
【发布时间】:2014-10-15 11:13:00
【问题描述】:

我在尝试将同名字段保存到新的单独记录时遇到问题。

似乎只将最后输入的字段(第二个输入)保存到新记录,而不是将第一个输入的字段保存到新记录。

我需要将两个输入的字段另存为新的单独记录

查看代码 -

<div class="row">
<div class="col-xs-12">
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="fa fa-barcode"></i>Oven 1</div>
            <div class="tools">
                <a href class="collapse"></a>
            </div>
        </div>
        <div class="portlet-body form">
            <div class="form-body">
                <div class="row">                   
                    <div class="col-xs-12 col-sm-3">
                        <div class="form-group">
                        <?php echo Form::label('height', 'height', array('class' => 'col-md-3 control-label')) ?>
                            <div class="col-md-9">
                                <?php echo Form::input('height[1]', $check->height, array('id' => 'height', 'class' => 'form-control')) ?>
                            </div>
                        </div>
                    </div>                                                 
                </div>                     
            </div>
        </div>           

        <div class="portlet-body form">
            <div class="form-body">
                <div class="row">                   
                    <div class="col-xs-12 col-sm-3">
                        <div class="form-group">
                        <?php echo Form::label('height', 'height', array('class' => 'col-md-3 control-label')) ?>
                            <div class="col-md-9">
                                <?php echo Form::input('height[2]', $check->height, array('id' => 'height', 'class' => 'form-control')) ?>
                            </div>
                        </div>
                    </div>                                                 
                </div>                     
            </div>
        </div>
    </div>
</div>

控制器代码 -

    public function action_clean()
{
    $id = $this->request->param('id');

    if ($_POST) 
    {             
        $check = $cat->height->find();

        foreach($_POST['height'] as $key=>$value){ 

            $check->height = $value;           
        }

        $check->dt_logged = time();                
        $check->user_id = Auth::instance()->get_user()->id;                
        $check->cat = $cat->id;          
        $check->save();

        echo json_encode(array('id' => $cat->id));            
    }

}

回顾一下,我需要将两个输入字段保存为新的单独记录

任何帮助将不胜感激。

干杯

【问题讨论】:

    标签: php html kohana


    【解决方案1】:

    我认为这是因为您只是在循环中覆盖了 height 变量,最后一个值将保留在其中,而不是将其保存在循环中。试试:

     foreach($_POST['height'] as $key=>$value){ 
    
        $check->height = $value;
        $check->dt_logged = time();                
        $check->user_id = Auth::instance()->get_user()->id;                
        $check->cat = $cat->id;          
        $check->save();         
    }
    

    或者,如果您希望一切都相同,那么除了高度之外:

        $check->dt_logged = time();                
        $check->user_id = Auth::instance()->get_user()->id;                
        $check->cat = $cat->id;
        foreach($_POST['height'] as $key=>$value){ 
    
    
            $check->height = $value;
            $check->save();         
        }
    

    【讨论】:

    • 没有保存到数据库中?
    • 与最初的问题相同。仅将第二个输入保存为新记录,而不是将第一个输入保存为新记录。
    • 我忘了把这行放到循环里:echo json_encode(array('id' =&gt; $cat-&gt;id));我不知道,为什么它不起作用。
    • 这很酷,无论如何感谢您的帮助!我将重新设计我的数据库。
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多