donglisi8644 2013-02-20 19:21
浏览 70
已采纳

将键值对数组插入mysql数据库

Hello quick question for you guys/gals

I have a javascript object that displays as follows in the console.

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

Passing this object to the backend CI Controller thru AJAX I've created the following function with 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.
  }
 }
}

the response from php for the print_r is as follows

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

I would like to push each element and value to a new row as follows.

EMAIL_TABLE

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

Where the 'id' is auto incremented how ever 'cus_id' and 'email' are read and stored from the array.

Any help or directions to helping me solve this would be greatly appreciated!

Thank you for reading, Regards.

  • 写回答

2条回答 默认 最新

  • douzai6337 2013-02-20 19:23
    关注

    Set id as an auto_increment primary key column in your table.

    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 will then take care of itself, all you have to do is leave it alone when inserting.

    PHP code to do this in a loop:

    foreach ($test as $cusid => $email) {
        $querystring = "insert into users (cus_id, email) values ('$cusid', '$email')";
        myqsli_query($querystring);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验