Mysql
 sql >> Database >  >> RDS >> Mysql

Array - Inserisci i valori dell'array nella tabella MySQL

Prova a utilizzare un ciclo for each invece di while

data[0][firstName]  James
data[0][lastName]   Smith
data[1][firstName]  Jane
data[1][lastName]   Doe
data[2][firstName]  Bob 
data[2][lastName]   Builder
etc..

foreach($data as $d){
    $person_fname = $d['firstName'];
    $person_lname = $d['lastName'];

    $query_init2 = "INSERT INTO person ( first_name, last_name) VALUES (:person_fname,:person_lname);";
    $query_prep2 = $db_con->prepare($query_init2);
    $insert_result2 = $query_prep2->execute(array(
      "person_fname" => $person_fname,
      "person_lname" => $person_lname
    ));
}