如何使用 PHP 检查用户是否已存在于 MySQL 中

2021-12-25 00:00:00 php mysql mysqli

我正在使用以下对我不起作用的代码.

$con=mysqli_connect("localhost","root","","my_db");$check="SELECT COUNT(*) FROM people WHERE Email = '$_POST[eMailTxt]'";如果 (mysqli_query($con,$check)>=1){echo "用户已经存在<br/>";}别的{$newUser="INSERT INTO people(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";如果 (mysqli_query($con,$newUser)){echo "您现在已注册<br/>";}别的{echo "在数据库中添加用户时出错<br/>";}}

<块引用>

mysqli_result 类的对象无法转换为 int inC:xampphtdocsExpwelcome.php

解决方案

这段代码很适合你...

$con=mysqli_connect("localhost","root","","my_db");$check="SELECT * FROM people WHERE Email = '$_POST[eMailTxt]'";$rs = mysqli_query($con,$check);$data = mysqli_fetch_array($rs, MYSQLI_NUM);如果($数据[0]> 1){echo "用户已经存在<br/>";}别的{$newUser="INSERT INTO people(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";如果 (mysqli_query($con,$newUser)){echo "您现在已注册<br/>";}别的{echo "在数据库中添加用户时出错<br/>";}}

I am using following code which is not working for me.

$con=mysqli_connect("localhost","root","","my_db");
$check="SELECT COUNT(*) FROM persons WHERE Email = '$_POST[eMailTxt]'";
if (mysqli_query($con,$check)>=1)
{
    echo "User Already in Exists<br/>";
}
else
{
    $newUser="INSERT INTO persons(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";
    if (mysqli_query($con,$newUser))
    {
        echo "You are now registered<br/>";
    }
    else
    {
        echo "Error adding user in database<br/>";
    }
}

Object of class mysqli_result could not be converted to int in C:xampphtdocsExpwelcome.php

解决方案

this code works fine for you...

$con=mysqli_connect("localhost","root","","my_db");
$check="SELECT * FROM persons WHERE Email = '$_POST[eMailTxt]'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
    echo "User Already in Exists<br/>";
}

else
{
    $newUser="INSERT INTO persons(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";
    if (mysqli_query($con,$newUser))
    {
        echo "You are now registered<br/>";
    }
    else
    {
        echo "Error adding user in database<br/>";
    }
}

相关文章