Sunday, March 17, 2013

Save array as multiple fields in DB

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);
}

No comments:

Post a Comment