0

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.

6
  • 1
    Based on $query1 = $result_array1[$i]["emailaddress"]; it looks to me like $query1 would be a string Commented Mar 3, 2020 at 19:44
  • $query1 is not an array. It's the email address of the last user, so it's a string. Commented Mar 3, 2020 at 19:45
  • Why are you querying SELECT emailaddress FROM users in a loop? Commented Mar 3, 2020 at 19:45
  • @Don'tPanic yes but when i leave out "[$i]["emailaddress"]" it always opts to else ... Commented Mar 3, 2020 at 19:46
  • 1
    What sort of number of addresses are added in each call (10's, 1000's)? Commented Mar 3, 2020 at 19:49

1 Answer 1

2

$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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried this code, but now i get a dozen of the exact same warnings saying >Notice: Array to string conversion in ... addingusers.php on line 29
Which line is that?
...with line 29 being $existing_addresses = array_intersect($emailaddress, $result_array1);
It shows: array(1) { ["emailaddress"]=> string(12) "[email protected]" }
Looks like you did $result_array1[] = $row1; instead of $result_array1[] = $row1['emailaddress'];
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.