Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在此处成功使用函数数组_intersect()?_Php_Array Intersect - Fatal编程技术网

Php 如何在此处成功使用函数数组_intersect()?

Php 如何在此处成功使用函数数组_intersect()?,php,array-intersect,Php,Array Intersect,我正在尝试制作一个系统,管理员可以同时将多人添加到数据库中。我希望此系统阻止管理员添加数据库中已存在电子邮件地址的用户 如果_POST[emailaddress]中的一封电子邮件与数据库中的一封电子邮件地址匹配,用户应该会收到一条消息,说明数据库中已经存在一封电子邮件。为了实现这一点,我尝试使用函数数组_intersect。然而,在这样做的时候,我得到一个警告说: 警告:数组_intersect:参数2不是。。。第41行的addingusers.php 起初我认为这与我的第二个参数是一个关联数组

我正在尝试制作一个系统,管理员可以同时将多人添加到数据库中。我希望此系统阻止管理员添加数据库中已存在电子邮件地址的用户

如果_POST[emailaddress]中的一封电子邮件与数据库中的一封电子邮件地址匹配,用户应该会收到一条消息,说明数据库中已经存在一封电子邮件。为了实现这一点,我尝试使用函数数组_intersect。然而,在这样做的时候,我得到一个警告说:

警告:数组_intersect:参数2不是。。。第41行的addingusers.php

起初我认为这与我的第二个参数是一个关联数组这一事实有关,所以我尝试了函数array_intersect_assoc,它返回相同的警告。我怎样才能解决这个问题

addingusers.php上的代码

编辑 正如注释所指出的,$query1实际上不是一个数组,而是一个字符串。但是,即使我删除了索引和[emailaddress],问题仍然存在,如中所示,代码总是选择else语句,而从不选择。如果。

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

你也有比你需要更多的循环。您不需要在循环中执行SelectEmailAddressfromUsers查询,也不需要在循环中检查交叉点。因为你不需要这些循环,你不需要先得到计数

<?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;
}
$query1不是数组,它只是一个电子邮件地址。您应该在循环中推动它,而不是覆盖它

你也有比你需要更多的循环。您不需要在循环中执行SelectEmailAddressfromUsers查询,也不需要在循环中检查交叉点。因为你不需要这些循环,你不需要先得到计数

<?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;
}

基于$query1=$result_array1[$i][emailaddress];在我看来,$query1应该是字符串,$query1不是数组。这是最后一个用户的电子邮件地址,所以它是一个字符串。为什么要在循环中查询用户的SELECT emailaddress?@Don'tPanic yes,但当我省略[$i][emailaddress]时,它总是选择else…每次调用10、1000个时会添加多少个地址?基于$query1=$result_array1[$i][emailaddress];在我看来,$query1应该是字符串,$query1不是数组。这是最后一个用户的电子邮件地址,所以它是一个字符串。为什么要在循环中查询用户的SELECT emailaddress?@Don'tPanic yes,但当我省略[$i][emailaddress]时,它总是选择else…每次呼叫中添加的地址数是多少?10个,1000个?我已经尝试过这个代码,但现在我收到了十几条完全相同的警告,上面写着>注意:数组到字符串的转换。。。第29行的addingusers.php是哪一行?…第29行是$existing\u addresses=array\u intersect$emailaddress,$result\u array1;它显示:array1{[emailaddress]=>string12test@test.co}看起来您执行了$result_array1[]=$row1;而不是$result_array1[]=$row1['emailaddress'];我尝试过这段代码,但现在我收到了十几条完全相同的警告,它们是>注意:数组到字符串的转换在。。。第29行的addingusers.php是哪一行?…第29行是$existing\u addresses=array\u intersect$emailaddress,$result\u array1;它显示:array1{[emailaddress]=>string12test@test.co}看起来您执行了$result_array1[]=$row1;而不是$result_array1[]=$row1['emailaddress'];