I have multidimentions array hrefs :
Array
(
[1] => Array
(
[0] => http://213b572-ba681bf9cc9e
[1] => http://f057-4139-ac40-bc4449722ffc
[2] => http://b-c151-4ba1-b7b7-842771c36d6b
[3] => http://5a77fb-8fce-4793-868f-c9fd73524037
)
[2] => Array
(
[0] => http://8-d832-4b34-a55b-da04ad8cdd09
[1] => http://b38-6a60-4233-b207-f40fae2ef431
[2] => http://3-f31c-49c4-87ee-fcada05a105f
[3] => http://07514-e438-45e2-906e-b440cbcbf8dc
)
......
[76] => Array
(
[0] => http://8-d832-4b34-a55b-da04ad8cdd09
[1] => http://b38-6a60-4233-b207-f40fae2ef431
[2] => http://3-f31c-49c4-87ee-fcada05a105f
[3] => http://07514-e438-45e2-906e-b440cbcbf8dc
)
When i insert array hrefs above into database
foreach ($hrefs as $id_page => $href) {
foreach ($href as $value) {
mysqli_query($con, "INSERT INTO urls(`id`, `id_page`, `url`)
VALUES ('', '$id_page', '$value')");
}
}
mysqli_close($con);
I want my database are:
| id | id_page | url |
| 1 | 1 | http://jjjjjjjjj |
| 2 | 1 | http://jjjjjjjjj|
| 3 | 1 | http://jjjjjjjjj|
......
| 1000 | 76 | http://jjjjjjjjj|
but result:
| id | id_page | url |
| 1 | 1 | http://jjjjjjjjj |
| 2 | 1 | http://jjjjjjjjj|
| 3 | 1 | http://jjjjjjjjj|
......
| 500 | 35 | http://jjjjjjjjj|
| 501 | 1 | http://jjjjjjjjj|
When insert id_page loop to 35 and return begin 1. $hrefs is multidimentions array like above. Any resolve?
I would try it this way :
foreach ($hrefs as $id_page => $href) {
foreach ($href as $value) {
mysqli_query($con, "INSERT INTO urls(`id_page`, `url`)
VALUES ('".$id_page."', '".$value."')");
}
}
mysqli_close($con);
Assuming "id" is set to auto_increment in your table.
Edit1: Check your data type in your "urls" table: maybe your "id_page" is too small to hold big values. Set it to int(11) for example.
Edit2: If there is any performance issue, you should execute only one query with multiple values, like that:
$sql = 'INSERT INTO urls(`id_page`, `url`) VALUES ';
foreach ($hrefs as $id_page => $href) {
foreach ($href as $value) {
$sql .= "('".$id_page."','".$value."'),";
}
}
$sql = substr($sql, 0, -1); // delete last comma
mysqli_query($con, $sql);
mysqli_close($con);