Let's say I have value for user,form,and ownerID. The value for user stays the same however ownerID might vary from time to time.
for example:
$user = "Farhana"; // fix value
$form = "Registration"; // fix value
$ownerID = array("23","50","33"); // item in array not fix, may have many ownerID
then I want to insert multiple values with single sql statement like this.
INSERT INTO messages (user,ownerID,form) VALUES ("Farhana","23","Registration"),("Farhana","50","Registration"),("Farhana","33","Registration");
so I have come out with foreach solution but I'm not sure this is correct or not.
// prepare
$stmt = $this->conn->prepare("INSERT INTO messages (user,ownerID,form) VALUES (?,?,?)");
// bind param
foreach ($ownerID as $ID){
$stmt->bind_param("sss",$user,$ID,$form);
$stmt->execute();
}
It's not really important, but just for sake of clarity bind_param have to be called only once.
$stmt = $this->conn->prepare("INSERT INTO messages (user,ownerID,form) VALUES (?,?,?)");
$stmt->bind_param("sss",$user,$ID,$form);
foreach ($ownerID as $ID){
$stmt->execute();
}