Det skulle vara mer effektivt att bygga en sats med WHERE IN
för att få matchande e-postmeddelanden och sedan jämföra de resulterande arrayerna. Du kan använda array_diff()
för att få skillnaden mellan arrayerna.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $username, $password, $db);
echo "Connected successfully\n";
$tags = preg_split("/\,/", $_POST['tags']);
$invalidEmails = array();
$count = 0;
// modify your array how you want it
foreach ($tags as $i => $tag) {
$trim_brackets = trim($tag, '[]');
$trim_quotes = trim($trim_brackets, '"');
$tags[$i] = $trim_quotes;
}
// build placeholders for WHERE IN
$in = str_repeat('?,', count($tags) - 1) . '?';
// prepare query
$stmt = $conn->prepare("SELECT mail FROM dej_colleagues WHERE mail IN ($in)");
// bind an array of values. First params is a type list.
$stmt->bind_param(str_repeat('s', count($tags)), ...$tags);
$stmt->execute();
$results = $stmt->get_result();
// fetch the matching mails into an array
$mails = [];
foreach ($results as $row) {
$mails[] = $row['mail'];
}
// get the difference between the two arrays
$invalidEmails = array_diff($tags, $mails);
if (count($tags) === count($mails)) {
echo "good";
}
var_dump($invalidEmails);