Hello,

I tried to verify if users or record already exist in the database so to do that I used a concept of in array fucntion but this was not successfull

here is my code

function adduser($connect) {
        $uname  = $_POST['uname'];
        $pass   = $_POST['pass'];
        $email  = $_POST['email'];

        $query_get  = mysqli_query($connect, "SELECT * FROM users");
        while($row = mysqli_fetch_array($query_get)) {
            $username = $row["username"];
        }   
            if(in_array($uname, $username, TRUE)) {
                $_SESSION["msg"] = "This user is already registered Please try a different username";
            } else {
                $query  = "INSERT INTO users (username, password, email) VALUES ('$uname', '$pass', '$email')";

                $insert = mysqli_query($connect, $query);

                if($insert) {
                    $_SESSION["msg"] = "You have successfully registered";
                    header("Location: register.php");
                    exit();
                } else {
                    $_SESSION["msg"] = "There were some errors while registering. Please try again";
                    header("Location: register.php");

                }
            }                 
    }

My error

Warning: in_array() expects parameter 2 to be array, string given in /home/....

Dani AI

Generated

The warning comes from passing a string where an array is required: the loop in the original post leaves $username as a single string (the last row fetched), so in_array() complains. spotted the overwrite; noted the needle vs haystack order; and pointed toward doing the check in SQL. The most reliable fixes are (1) test existence in the database with a parameterized query, or (2) explicitly build an array of usernames before using in_array().

A safe, efficient pattern (preferred) — check for an existing username with a prepared statement and insert with a hashed password:

$stmt = $connect->prepare('SELECT 1 FROM users WHERE username = ? LIMIT 1');
$stmt->bind_param('s', $uname);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows > 0) {
    $_SESSION['msg'] = 'This username is already taken';
} else {
    $hash = password_hash($pass, PASSWORD_DEFAULT);
    $ins = $connect->prepare('INSERT INTO users (username, password, email) VALUES (?, ?, ?)');
    $ins->bind_param('sss', $uname, $hash, $email);
    $ins->execute();
}

If an array approach is desired (to use in_array()), fetch the column into an array first — do not overwrite a scalar variable in the loop:

$res = mysqli_query($connect, 'SELECT username FROM users');
$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
$usernames = array_column($rows, 'username');

if (in_array($uname, $usernames, true)) { /* duplicate */ }

Extra notes: enforce uniqueness at the DB level with a UNIQUE index, always use prepared statements to avoid SQL injection, store passwords with password_hash()/password_verify(), normalize case if usernames should be case-insensitive, and call exit() immediately after header('Location: ...') to stop execution. These measures make the check robust and secure compared with scanning all rows in PHP.

Recommended Answers

All 8 Replies

try to skip the TRUE?

if(in_array($uname, $username)) {

removed and having same error

while ($row = mysqli_fetch_array($query_get)) {
    $username = $row['username'];
} 

THAT keeps overwriting your username variable, so it's not an array. This is how you build an array.

while($row = mysqli_fetch_array($query_get)) {
    $username[] = $row['username'];
} 

I wonder why you are doing this, instead of just querying for the username...

Any suggestion you would like to make for me acctually want to check if the username is already been created so on that move user will see an error message username already exist please ,et chek

Any suggestion you would like to make for me

I already did:

querying for the username

Use the SELECT query to check if the username exists, instead of getting all users and then doing it in PHP. Add a WHERE clause. There are plenty examples in this forum already.

if(in_array($uname, $username, TRUE))

specify the needle or searched value, just like here below

$uname='user@email.com';
if(in_array($uname, $username, TRUE))

I hope you will succeed...

Something like this,

$query_get  = mysqli_query($connect, "SELECT count(*) as total FROM users where user_email='$email' OR user_name='$uname'");

list($total) = mysqli_fetch_row($query_get);

if ($total > 0){
    $_SESSION["msg"] = "This user is already registered Please try a different username";
    }

 if(empty($_SESSION["msg"])) {
 ....}

ohh yes thank you all I suceeded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.