I have a loop foreach in it, I execute queries. I have an associative array, the key is the name of the column. If id is not AUTO_INCREMENT, only the first request is performed true, and all the other false. If you put an AUTO_INCREMENT in db, all queries are executed, but in a cascade style.
$namePhones = ["phone_1", "phone_2", "phone_3", "phone_4", "phone_5"];
$jsonPhones = $userInfo;
$jsonPhones = array_splice($jsonPhones, 0, 1);
$phones = new stdClass();
$phonesCount = count($jsonPhones[0]);
foreach ($jsonPhones[0] as $key => $value) {
if($key < $phonesCount){
$phones->{$namePhones[$key]} = $jsonPhones[0][$key];
} else { return; }
}
$phonesDB = json_decode(json_encode($phones), true);
//$phonesDB//this is array
// Array
// (
// [phone_1] => 1
// [phone_2] => 2
// [phone_3] => 3
// [phone_4] => 4
//)
foreach ($phonesDB as $key => $value) {
$queryInsertPhones =
"INSERT INTO
`phones_users` ($key)
VALUES ($value)";
$resultPhones = mysqli_query($con, $queryInsertPhones);
}
cascade style.
+--------------------------------+
|id | phone_1 | phone_2 | phone_3|
|---+---------+---------+--------|
|1 | 1 | --- | --- |
|---+---------+---------+--------|
|2 | --- | 2 | --- |
|---+---------+---------+--------|
|3 | --- | --- | 3 |
+---+----------------------------+
I want this.
+--------------------------------+
|id | phone_1 | phone_2 | phone_3|
|---+---------+---------+--------|
|1 | 1 | 2 | 3 |
|---+---------+---------+--------|
|2 | --- | --- | --- |
+---+----------------------------+