0

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

4
Contributors
8
Replies
22
Views
2 Years
Discussion Span
Last Post by UK-1991
0
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...

0

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

0

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.

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

0

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"])) {
 ....}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.