Borja Borja - 3 months ago 13
MySQL Question

How to add id (primary key) when insert a multi-dimensional php array into a mysql table?

I created this mysql table:

CREATE TABLE earthquakes (
id INT(20) AUTO_INCREMENT,
idserial VARCHAR(30),
milliseconds BIGINT,
latitude FLOAT,
longitude FLOAT,
magnitude FLOAT,
ipocentro FLOAT,
source VARCHAR(4),
region SMALLINT,
PRIMARY KEY(id),
INDEX indice_idserial(idserial),
INDEX indice_milliseconds(milliseconds),
INDEX indice_lat_lng(latitude,longitude),
INDEX indice_magnitude(magnitude),
INDEX indice_ipocentro(ipocentro)
);


And i want to insert a multi-dimensional php array into a mysql table but i dont'know like to add also
id
.

This is code to add array:

//MULTIDIMENSIONAL ARRAY
$array_database= array(
array("2017-06-30-104",1498858541000,39.3322,-122.9027,2.11,0,"U",36)
);
//

foreach($array_database as $row) {
$idserial_db= mysql_real_escape_string($row[0]);
$milliseconds_db = mysql_real_escape_string($row[1]);
$latitude_db = mysql_real_escape_string($row[2]);
$longitude_db = mysql_real_escape_string($row[3]);
$magnitude_db = mysql_real_escape_string($row[4]);
$ipocentro_db= mysql_real_escape_string($row[5]);
$source_db= mysql_real_escape_string($row[6]);
$region_db= mysql_real_escape_string($row[7]);

$valori_db[] = "('$idserial_db',$milliseconds_db,$latitude_db,$longitude_db,$magnitude_db,$ipocentro_db,'$source_db',$region_db)";
}
$values = implode(',', $valori_db);


mysqli_query($connessione,"INSERT INTO earthquakes (idserial,milliseconds,latitude,longitude,magnitude,ipocentro,source,region) VALUES $values");


So how i add
ID
?

Thanks a lot and sorry for my english

EDIT
Maybe i should to add
ID
in this way:

mysqli_query($connessione,"INSERT INTO earthquakes (id,idserial,milliseconds,latitude,longitude,magnitude,ipocentro,source,region) VALUES $values");

Answer Source

In the declaration:

//MULTIDIMENSIONAL ARRAY
$array_database= array(
 array("", "2017-06-30-104",1498858541000,39.3322,-122.9027,2.11,0,"U",36)
);
//

In the loop:

 $valori_db[] = "    ('','$idserial_db',$milliseconds_db,$latitude_db,$longitude_db,$magnitude_db,$ipocentro_db,'$source_db',$region_db)";

In the query:

mysqli_query($connessione,"INSERT INTO earthquakes  (id,idserial,milliseconds,latitude,longitude,magnitude,ipocentro,source,region) VALUES $values");