简体   繁体   English

检查数据库中是否存在用户名(PHP)

[英]Check if username exists in database (PHP)

I think the following code should do the trick, but it still doesn't detect whether the username exists or not.我认为以下代码应该可以解决问题,但它仍然无法检测用户名是否存在。

Here is my code:这是我的代码:

//CHECK IF USERNAME ALREADY EXISTS
$query = "SELECT * FROM users WHERE username=" . $_POST['username'];
$result = @mysqli_query($dbc, $query);
if(mysqli_num_rows($result >= 1))
{
    echo '<br><br><span style="color:red;text-align:left;">This userame has already been taken!</span>';
    exit;
}

Anyone know what I'm doing wrong / missing here?有人知道我做错了什么/在这里失踪了吗?

I think you should write it like this:我认为你应该这样写:

if(mysqli_num_rows($result) >= 1)

instead of代替

if(mysqli_num_rows($result >= 1))

mysqli_num_rows returns an int, so in your if statement you have to place a ) after $result mysqli_num_rows返回一个 int,所以在你的 if 语句中你必须在$result之后放置一个)

I think you should write it like this:我认为你应该这样写:

$query = "SELECT * FROM users WHERE username=" . $_POST['username'];

if ($result = mysqli_query($dbc, $query)) {

     echo '
           <br><br>
           <span style="color:red;text-align:left;">
               This userame has already been taken!
           </span>';
     exit;
}

You missed some quotes and singlequotes.你错过了一些引号和单引号。

$query = "SELECT * FROM `users` WHERE `username` = '" . $_POST['username'] ."'";
$result = mysqli_query($dbc, $query) OR DIE(mysqli_error($dbc)); // Dies if there is any error #debugging
$result_rows = mysqli_num_rows($result);
if ($result_rows >= 1) {
    echo '</br></br><span style="color:red;text-align:left;">This userame has already been taken!</span>';
    exit;
}

Using username = '" . $_POST['username'] ."';使用username = '" . $_POST['username'] ."'; instead of " . $_POST['username'];而不是" . $_POST['username'];

Then added some debug to the query using OR DIE(mysqli_error($connection));然后使用OR DIE(mysqli_error($connection));在查询中添加了一些调试OR DIE(mysqli_error($connection)); . .

The sql is written incorrectly. sql 写错了。 The closing double quotes should be outside the closing bracket instead of outside the equal sign.结束双引号应该在结束括号之外而不是在等号之外。 You should have this: $query = "SELECT * FROM users WHERE username= ' ".$_POST['username'] .你应该有这个: $query = "SELECT * FROM users WHERE username=' ".$_POST['username'] 。 " ' "; " ' "; Instead of this:取而代之的是:

$query = "SELECT * FROM users WHERE username=" . $query = "SELECT * FROM users WHERE username=" 。 $_POST['username']; $_POST['用户名'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM