【问题标题】:php array dropdown field not insert into database mysqlphp数组下拉字段未插入数据库mysql
【发布时间】:2014-03-13 03:01:42
【问题描述】:

我是 OOP PHP 的新手,我还在学习,我有一个表单,其中位置名称通过 JQuery/HTML 表单使用同名的下拉字段填充,我的问题是当我尝试插入 mysql 数据库时,我没有得到选中的值,

        <div id="NCR" class="drop-down-show-hide">  
        <div class="field">
            <label for="region"> NCR </label>
            <select name="region" id="ncr region">
                <option value="">-- Choose --</option>
                <option value="1"> Region 1 </option>
                <option value="2"> Region 2 </option>
                <option value="3"> Region 3 </option>
            </select> 
        </div>  
    </div>


    <div id="CAR" class="drop-down-show-hide">
        <div class="field">
            <label for="field"> CAR </label>
            <select name="region" id="car region">
                <option value="">-- Choose --</option>
                <option value="4"> Region 4 </option>
                <option value="5"> Region 5 </option>
                <option value="6"> Region 6 </option>
            </select> 
        </div>  
    </div>


    <div id="region3" class="drop-down-show-hide">
        <div class="field">
            <label for="region"> CAMANABA </label>
            <select name="region" id="camanaba region">
                <option value="">-- Choose --</option>
                <option value="7"> Region 7 </option>
                <option value="8"> Region 8 </option>
                <option value="9"> Region 9 </option>
            </select> 
        </div>          
    </div>/HTML

PHP

if(Token::check(Input::get('token'))){
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'apz_account' => array(
            'required' => true          
        ),
        'api_account' => array(
            'required' => true                      
        ),
        'wupos_tid' => array(
            'required' => true      
        ),
    ));

    if($validation->passed()){

        // check APZ account 
        $agent = DB::getInstance()->get('agent', array('apz_account', '=', Input::get('apz_account')));

        if(!$agent->count()){

                // check API account 
                $agent = DB::getInstance()->get('agent', array('api_account', '=', Input::get('api_account')));

                if(!$agent->count()){

                    $agent = new Agent();

                    try {


                        $agent->createAgentAccount(array(
                            'apz_account' => Input::get('apz_account'),
                            'api_account' => Input::get('api_account'),
                            'wupos_tid' => Input::get('wupos_tid'),
                            'region' => Input::get('region'),
                            'registered' => date('Y-m-d H:i:s'),
                        ));


                        //  print_r($agent);
                         // Session::flash('success', 'You have registered successfully.');
                         // Redirect::to('../../index.php');


                        } catch(Exception $e){
                            die($e->getMessage());
                    }



                    } else {

                    echo Input::get('api_account'), ' account is already exists';
                }

            } else {

            echo Input::get('apz_account'), ' account is already exists';
        }


    } else {
        foreach ($validation->errors() as $error) {
            echo $error, '<br>';    
        }

    }
}

请指教。谢谢

【问题讨论】:

标签: php jquery mysql


【解决方案1】:

您需要为每个表单元素提供唯一的名称,使用 php 您可以使用region[]。这将在您的服务器脚本中为您提供一个数组。

然后当你得到值时做current(array_filter(Input::get('region')));

【讨论】:

  • 嗨,谢谢你的评论,无论如何它在错​​误中说:警告:array_filter() 期望参数 1 是数组,给定字符串和 urrent() 期望参数 1 是数组,在中给出 null 跨度>
  • @JaysonLacson 您是否将选择表单元素的名称从 region 更改为 region[]
  • 嗨 @Preston 是的,它已更改为 region[]。
猜你喜欢
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 2016-05-26
相关资源
最近更新 更多