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

将键值对数组插入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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?