Ich habe folgendes Beispiel getestet und es sind keine Duplikate aufgetreten:
class Franchises
{
protected static function getDB()
{
$conn = NULL;
try
{
$conn = new PDO("mysql:host=127.0.0.1;dbname=franchises_db", "dbuser", "dbpassword");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
exit();
}
return $conn;
}
public static function setLeadData($franchises)
{
try
{
$db = static::getDB();
$sql = "INSERT INTO leads_franchises SET
franchise_id = :franchise_id,
franchisor_id = :franchisor_id,
franchise_name = :franchise_name";
$stmt = $db->prepare($sql);
foreach($franchises as $franchise)
{
$result = $stmt->execute([
':franchise_id' => $franchise['franchise_id'],
':franchisor_id' => $franchise['franchisor_id'],
':franchise_name' => $franchise['franchise_name']
]);
}
return $result;
}
catch(PDOException $e)
{
echo $e->getMessage();
exit();
}
}
}
$vals = [
[
"franchise_id" => 3,
"franchisor_id" => 3,
"franchise_name" => "Fitness Freaks"
],
[
"franchise_id" => 4,
"franchisor_id" => 3,
"franchise_name" => "Gyms Galore"
]
];
Franchises::setLeadData($vals);
und der Erstellungscode für die Tabelle lead_franchises lautet:
CREATE TABLE `leads_franchises` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`franchise_id` INT(11) NULL DEFAULT '0',
`franchisor_id` INT(11) NULL DEFAULT '0',
`franchise_name` VARCHAR(128) NULL DEFAULT '',
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;