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/....

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.