I cannot figure out why the foreach loop iterates more times than the count of elements.
The function passes two associative arrays: form data and franchise data. The form data elements are converted to variables. I want to loop through the franchise array (unknown length) and insert the form data for each unique franchise ID. Each record will contain identical form data (from user) but different data for the franchise_id, franchisor_id and franchise_name fields.
What's happening is that if one franchise is selected two identical records are inserted. If two, then a total of four are inserted (duplicate set of the two). However, if three are selected, it inserts nine rows (three sets of the three choices). This, despite the array count being 3.
I have modified the code countless times without success.
Here's the function:
public static function setLeadData($franchises, $formData)
{
$first_name = $formData[0]['value'];
$last_name = $formData[1]['value'];
$telephone = $formData[2]['value'];
$email = $formData[3]['value'];
$zipcode = $formData[4]['value'];
$networth = $formData[5]['value'];
$liquidcapital = $formData[6]['value'];
$message = $formData[7]['value'];
try
{
$db = static::getDB();
$sql = "INSERT INTO leads_franchises SET
franchise_id = :franchise_id,
franchisor_id = :franchisor_id,
franchise_name = :franchise_name,
first_name = :first_name,
last_name = :last_name,
telephone = :telephone,
email = :email,
zipcode = :zipcode,
networth = :networth,
liquidcapital = :liquidcapital,
message = :message";
$stmt = $db->prepare($sql);
foreach($franchises as $franchise)
{
$parameters = [
':franchise_id' => $franchise['franchise_id'],
':franchisor_id' => $franchise['franchisor_id'],
':franchise_name' => $franchise['franchise_name'],
':first_name' => $first_name,
':last_name' => $last_name,
':telephone' => $telephone,
':email' => $email,
':zipcode' => $zipcode,
':networth' => $networth,
':liquidcapital' => $liquidcapital,
':message' => $message
];
$result = $stmt->execute($parameters);
}
return $result;
}
catch(PDOException $e)
{
echo $e->getMessage();
exit();
}
}
Here's the franchises[] with three selections:
[0] => Array
(
[franchise_id] => 2
[franchisor_id] => 3
[franchise_name] => Franchise One
[logo] => image2.jpg
[logo_thumb] => thumb_image2.jpg
[min_capital] => 200000
[description] => description
[website] => example2.com
[inquiry_email] => info@gmail.com
[created_at] => 2018-01-17 13:05:51
[updated_at] => 2018-01-25 17:09:51
)
[1] => Array
(
[franchise_id] => 3
[franchisor_id] => 3
[franchise_name] => Franchise Two
[logo] => image3.jpg
[logo_thumb] => thumb_image3.jpg
[min_capital] => 100000
[description] => description
[website] => example3.com
[inquiry_email] => info@gmail.com
[created_at] => 2018-01-21 18:03:39
[updated_at] => 2018-01-25 17:10:02
)
[2] => Array
(
[franchise_id] => 4
[franchisor_id] => 3
[franchise_name] => Franchise Three
[logo] => image4.jpg
[logo_thumb] => thumb_image4.jpg
[min_capital] => 100000
[description] => description
[website] => example4.com
[inquiry_email] => info@gmail.com
[created_at] => 2018-01-25 10:25:52
[updated_at] => 2018-01-25 17:58:08
)
If anyone can see my error(s) in the code or my approach, I would really appreciate it.