简体   繁体   中英

How do i successfully use function array_intersect() here?

I'm trying to make a system where an administrator can add multiple people at the same time into a database. I want this system to prevent the administrator from adding people with email addresses already existing in the database.

IF one of the emails in the _POST["emailaddress"] matches with one of the emailaddresses in the db, the user should get a message saying one of the emails already exists in the database. To achieve this, I've tried using the function array_intersect(). However, upon doing so I get a warning saying:

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

At first i thought it had something to do with the fact my second argument was an associative array, so I tried the function array_intersect_assoc, which returns the same warning. How can I solve this?

The code on 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;
    }
}

EDIT as the comments point out, $query1 is indeed not an array it is a string. However, the problem remains even IF i remove the index and "[emailaddress]", as in, the code always opts to the else-statement and never to if.

$query1 is not an array, it's just one email address. You should be pushing onto it in the loop, not overwriting it.

You also have more loops than you need. You don't need to perform SELECT emailaddress FROM users query in a loop, and you don't need to check the intersection in a loop. And since you don't need those loops, you don't need to get the count first.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM