1

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
5
  • How are you inserting data in mysql . What have you tried till now ? Commented Aug 15, 2021 at 6:38
  • I've found this example but it throws an error about array to string conversion: $consulta= "DECLARE @array varchar(max) = '($array)' SET @array = REPLACE( REPLACE(@array, ';', '), ('), ', ()', '') DECLARE @SQLQuery VARCHAR(MAX) = 'INSERT INTO hora (hora,link) VALUES ' + @array EXEC (@SQLQuery)"; Commented Aug 15, 2021 at 6:55
  • it would be better if you update your answer with the code you are using and the error you got Commented Aug 15, 2021 at 7:01
  • Just loop over the array to generate the VALUES sections of an INSERT statement (you can set multiple sets of values in an INSERT) and then execute that. You're aiming for this kind of structure INSERT INTO yourtable (colA, colB) VALUES (1, 2), (3, 4), (5, 6) Commented Aug 15, 2021 at 7:11
  • Please, if you found the solution, add it as an Answer. The solution is not part of the question. I'll give you a few minutes to sort it out and then roll back your edit. stackoverflow.com/help/self-answer . Glad you solved it anyway Commented Aug 15, 2021 at 7:37

1 Answer 1

1

You can just loop over the $array and then explode it again using , separator. Then you can run MySQL insert query to insert the data in your table.

Like

foreach($array as $row){
  $data = explode(',', $array);
  //Run MySQL query here to insert this data in your table
  $sql_query = "INSERT INTO hora (hora,link) VALUES ('".$data[0].'", '".$data[1].'") ";
 //Something like 
  $mysqli->query($sql_query);

}

Remember to always sanitize user data befor inserting it in database

Update: if using mysqli

$query = $mysql->prepare("INSERT INTO hora (hora,link) VALUES (?, ?)");
$query->bind_param("ss", $data[0], $data[1]);
$query->execute();

This will prevent SQL Injection attacks.

Sign up to request clarification or add additional context in comments.

2 Comments

I get Parse error: syntax error, unexpected identifier "in" in the line foreach($row in $array){
This is vulnerable to sql injection. You should show an example using parameters and prepared statements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.