0

こんにちは、みんな/ギャルへの簡単な質問

コンソールに次のように表示される JavaScript オブジェクトがあります。

Object { 10= "a@a.com", 18="b@b.com", 20="c@c.com"}

このオブジェクトを AJAX を介してバックエンド CI コントローラーに渡す CI を使用して次の関数を作成しました。

function NewEMail(){
 //this is the array coming from ajax
 $test = $this -> input -> post('sendValue');
 print_r($test);

 if(is_array($test)){
  foreach($test as $t){
  //Insert the values in to the db from here.
  }
 }
}

print_r に対する php からの応答は次のとおりです。

Array([10] => a@a.com [18] => b@b.com [20] => c@c.com)  

次のように、各要素と値を新しい行にプッシュしたいと思います。

EMAIL_TABLE

| id | cus_id | email   |  
| 1  | 10     | a@a.com |  
| 2  | 18     | b@b.com |   
| 3  | 20     | c@c.com | 

「id」は自動インクリメントされますが、「 cus_id 「email」は配列から読み取られて格納されます。

これを解決するための助けや指示は大歓迎です!

読んでくれてありがとう、よろしく。

4

2 に答える 2

0

これを試すことができます:

$test = $this->input->post('sendValue');

if( is_array($test) ){
    $data = array();
    foreach ($test as $cusid => $email) {
        $data[] = array('cus_id' => $cusid, 'email' => $email);
    }

    $this->db->insert_batch('tablename', data);
}
于 2013-02-21T07:41:14.280 に答える
0

テーブルの auto_increment 主キー列として id を設定します。

create table email (
    id int unsigned primary key auto_increment,
    cus_id int unsigned,
    email varchar(50)
);

insert into email (cus_id, email) values ('$cusid', '$email');

id はそれ自体を処理します。挿入するときにそれをそのままにしておくだけです。

ループでこれを行う PHP コード:

foreach ($test as $cusid => $email) {
    $querystring = "insert into users (cus_id, email) values ('$cusid', '$email')";
    myqsli_query($querystring);
}
于 2013-02-20T19:23:50.683 に答える