我如何在这里成功使用函数 array_intersect() ?

How do i successfully use function array_intersect() here?

我正在尝试制作一个管理员可以同时将多人添加到数据库中的系统。我希望这个系统能够防止管理员添加电子邮件地址已经存在于数据库中的人。

IF _POST["emailaddress"] 中的电子邮件之一与数据库中的电子邮件地址之一匹配,用户应该收到一条消息,说明其中一个电子邮件已存在于数据库中。为此,我尝试使用函数 array_intersect()。但是,这样做后我会收到一条警告:

Warning: array_intersect(): Argument #2 is not an array in ... addingusers.php on line 41

起初我认为这与我的第二个参数是关联数组这一事实有关,所以我尝试了函数 array_intersect_assoc,returns 同样的警告。我该如何解决?

代码在addingusers.php

<?php
session_start();
error_reporting(E_ALL); 
ini_set('display_errors',1);

$conn = mysqli_connect('localhost','*','*','*');
$condition = false; // this is for the other part of my code which involves inserting the output into db

$name = $_POST["name"];
$affix = $_POST["affix"];
$surname = $_POST["surname"];
$emailaddress = $_POST["emailaddress"];
$password = $_POST["password"];

//amount of emailaddresses in db
$checkquery2 = mysqli_query($conn, "
                SELECT COUNT(emailaddress)
                FROM users
                                        ");
$result2 = mysqli_fetch_array($checkquery2);

// the previously mentioned amount is used here below
for($i=0; $i<$result2[0]; $i++){
// the actual emails in the db itself
    $q1 = mysqli_query($conn, "
              SELECT
                    emailaddress
              FROM
                    users
            ");

// goes through all the emails 
    $result_array1 = array();
    while ($row1 = mysqli_fetch_assoc($q1)) {
        $result_array1[] = $row1;
    }

    $query1 = $result_array1[$i]["emailaddress"];
}

// HERE LIES THE ISSUE 
for($i=0; $i<count($emailaddress); $i++){
    if (count(array_intersect_assoc($emailaddress, $query1)) > 0) {
        echo "One of the entered emails already exists in the database...";
        echo '<br><button onclick="goBack()">Go Back</button>
    <script>
    function goBack() {
      window.history.back();
    }
    </script><br>';

        $condition = false;
    }
    else{
        $condition = true;
    }
}

编辑 正如评论所指出的那样,$query1 确实不是一个数组,而是一个字符串。但是,即使我删除了索引和“[emailaddress]”,问题仍然存在,因为代码总是选择 else 语句而不是 if.

$query1 不是一个数组,它只是一个电子邮件地址。您应该在循环中推动它,而不是覆盖它。

您的循环也比您需要的多。不需要循环执行SELECT emailaddress FROM users查询,也不需要循环检查交集。而且由于您不需要这些循环,因此您不需要先获取计数。

<?php
session_start();
error_reporting(E_ALL); 
ini_set('display_errors',1);

$conn = mysqli_connect('localhost','*','*','*');
$condition = false; // this is for the other part of my code which involves inserting the output into db

$name = $_POST["name"];
$affix = $_POST["affix"];
$surname = $_POST["surname"];
$emailaddress = $_POST["emailaddress"];
$password = $_POST["password"];

$q1 = mysqli_query($conn, "
      SELECT
            emailaddress
      FROM
            users
    ");

// goes through all the emails 
$result_array1 = array();
while ($row1 = mysqli_fetch_assoc($q1)) {
    $result_array1[] = $row1['emailaddress'];
}

$existing_addresses = array_intersect($emailaddress, $result_array1);
if (count($existing_addresses) > 0) {
    echo "Some of the entered emails already exists in the database: <br>" . implode(', ', $existing_addresses);
    echo '<br><button onclick="goBack()">Go Back</button>
    <script>
    function goBack() {
      window.history.back();
    }
    </script><br>';

    $condition = false;
}
else{
    $condition = true;
}