I found this type of question on stack overflow and other website but none of the solution work in my project.
I am working in Lumen Laravel micro-framework for API and I want to insert multiple record with single query.
below is the code for single record insert and working fine.This code is for follow category by user. So when user click on Follow Category button this query is run and insert one record in database with user_id and category_id field.
public function insertUserFollow($category){
$sql = " INSERT INTO news_user_category_following
SET
user_id = :user_id,
category_id = :category_id
";
$sth = $this->connection->prepare($sql);
$sth->bindParam(":user_id", $category['user_id']);
$sth->bindParam(":category_id", $category['category_id']);
$status = $sth->execute();
if(TRUE == $status){
return $status;
}else{
return FALSE;
}
}
Using above code my database looks like this :
user_id | category_id
------ | ------
1 | 5
Now How I want to insert is below :
user_id | category_id
------ | ------
1 | 5
1 | 2
1 | 3
1 | 6
1 | 4
1 | 1
So, I want multiple record to be inserted by single query and all data are coming dynamic so how can I manage for each look in query.