Does anyone have best practices when inserting an array into a database? I've used foreach loops with success in the past, as well as various array functions. Is there a better way?
Here is a sample code:
public function InsertRequests($id) {
$db = Database::getHandler();
$selected_requests = $this->GetRequests($this->allrequests);
foreach($selected_requests as $requestid) {
$sql = "INSERT INTO requests (userid,requestid,date) VALUES (?,?,NOW())";
$stmt = $db->prepare($sql);
$stmt->bindParam(1,$id,PDO::PARAM_STR);
$stmt->bindParam(2,$requestid,PDO::PARAM_STR);
$stmt->execute();
}
return true;
}
}
prepare()out of the loop, you don't need to prepare a newstmton each iteration, you only need to bind the params and execute.