I have this comma separated data, where rows are separated by ;
04:03:49,https://foo.bar/1; 04:03:34,https://foo.bar/2; 04:03:24,https://foo.bar/3; 04:03:09,https://foo.bar/4; 04:03:07,https://foo.bar/5; 04:03:07,https://foo.bar/6; 04:02:41,https://foo.bar/7;
And a mysql table like this one:
| time | link |
|---|---|
| 04:03:49 | https://foo.bar/1 |
| 04:03:34 | https://foo.bar/2 |
So im using this code to convert the data from $_POST to array:
$data=@$_POST['array'];
$array=explode(';', $data);
That results in:
Array (
[0] => 04:03:49,https://foo.bar/1
[1] => 04:03:34,https://foo.bar/2
[2] => 04:03:24,https://foo.bar/3
[3] => 04:03:09,https://foo.bar/4
[4] => 04:03:07,https://foo.bar/5
[5] => 04:03:07,https://foo.bar/6
[6] => 04:02:41,https://foo.bar/7
[7] =>
)
So, i need to insert that data into my db using the time in one column and the link in the other, been trying a few examples but can't seem to find the answer thanks in advance for the help.
I tried using this query:
$consulta= "DECLARE @array varchar(max) = '($array)'
SET @array = REPLACE( REPLACE(@array, ';', '), ('), ', ()', '')
DECLARE @SQLQuery VARCHAR(MAX) = 'INSERT INTO hora (hora,link) VALUES ' + @array
EXEC (@SQLQuery)";
But it errors with:
Warning: Array to string conversion in C:\xampp8\htdocs\plantas\index.php on line 138
$consulta= "DECLARE @array varchar(max) = '($array)' SET @array = REPLACE( REPLACE(@array, ';', '), ('), ', ()', '') DECLARE @SQLQuery VARCHAR(MAX) = 'INSERT INTO hora (hora,link) VALUES ' + @array EXEC (@SQLQuery)";INSERT INTO yourtable (colA, colB) VALUES (1, 2), (3, 4), (5, 6)