Php 检查是否存在帐户

Php check if exist account

不知道为什么这个检查甚至不显示一些回显文本...谁能解释一下原因。已发布所有代码,希望你现在能解决问题...尝试了你给我的所有代码,但仍然没有任何结果

<?php

$servername = "localhost";
$dbUsername = "db_admin";
$dbPassword = "123";
$dbName = "DB";
$dbTable = "login";

$salt = '';

$salt .= substr(str_shuffle(sha1(microtime())), 0, 40);;



$value_user =$_POST["usernameReg"];

$value_pass = $_POST["passwordReg"];
$value_pass = $salt . $value_pass . $salt;
$value_pass = sha1($value_pass);

$logCookieHashed = $salt . $value_user . $salt . $value_pass . $salt;
$logCookieHashed = sha1($logCookieHashed);



$conn = mysqli_connect($servername, $dbUsername, $dbPassword, $dbName);

$sqlGetID = "SELECT id FROM login ORDER BY id DESC LIMIT 1";

$result = mysqli_query($conn, $sqlGetID);

$row = mysqli_fetch_array($result);

$ID = $row['id'] + 1;

$addAcc = "INSERT INTO login (id, username, password, pwHash, logCookieHashed) Values (\"". $ID . "\", \"" . $value_user . "\", \"" . $value_pass . "\", \"" . $salt . "\", \"" . $logCookieHashed . "\");";

  if( isset( $_POST["usernameReg"]  ) ) {

$selectAcc = mysql_query("SELECT * FROM login WHERE username='$value_user'");
  if(mysql_num_rows($selectAcc)>0)
        {
            echo "Username already taken";
        }

 else   
    { 
    mysqli_query($conn, $addAcc);

      echo "You are now registered<br/>";
    }
    }

?>

可能有两个原因 1) mysql 和 mysqli 是两种不同的语法。 mysqli 是 mysql 的扩展版本。你混淆了两者。使用 mysql 或 mysqli 2)

  $sql = "select * from login where username='$value_user'";
  $selectAcc = mysqli_query($conn, $sql);
  $result= mysqli_num_rows($selectAcc);  
       if($result>1)
        {
            echo "Username already taken";
        }

     else      
      { 
        mysqli_query($conn, $addAcc);

        echo "You are now registered<br/>";
      }

}

Can you write the code how it should be ? – MisterTudi

应该是这样的。 使用 Prepared Statements,因为 mysql_ 函数已弃用.

//the values in $mysqli are serverIP,databaseUsername,databasePassword and databaseName
$mysqli = new mysqli('localhost','root','rootPassword','databaseName');
$stmt = $mysqli->prepare("SELECT * FROM login WHERE username = ?");
$stmt->bind_param('s',$value_user);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows() >= 1){
    echo 'username already taken.';
} else {
    echo 'username is good to go.';
}

根据你的originally posted question/code


试试下面的代码,这会解决你的问题。

$selectAcc="SELECT * FROM login WHERE username='$value_user'";
 $rsult=mysql_query($selectAcc);
$count=mysql_num_rows ($rsult); 
if($count==1){echo "username already in use"; }
else 
{ echo "username is available";}

这是您的代码的工作示例

<?php
$servername = "localhost";
$dbUsername = "db_admin";
$dbPassword = "123";
$dbName = "DB";
$dbTable = "login";

$salt = '';

$salt .= substr(str_shuffle(sha1(microtime())), 0, 40);;

header('Content-type: application/json');


$value_user =$_POST["usernameReg"];

$value_pass = $_POST["passwordReg"];
$value_pass = $salt . $value_pass . $salt;
$value_pass = sha1($value_pass);

$logCookieHashed = $salt . $value_user . $salt . $value_pass . $salt;
$logCookieHashed = sha1($logCookieHashed);



$conn = mysqli_connect($servername, $dbUsername, $dbPassword, $dbName);

$sqlGetID = "SELECT id FROM login ORDER BY id DESC LIMIT 1";

$result = mysqli_query($conn, $sqlGetID);

$row = mysqli_fetch_array($result);

$ID = $row['id'] + 1;

$addAcc = "INSERT INTO login (id, username, password, pwHash, logCookieHashed) Values (\"". $ID . "\", \"" . $value_user . "\", \"" . $value_pass . "\", \"" . $salt . "\", \"" . $logCookieHashed . "\");";
$checkUser = "SELECT username FROM login WHERE username = \"${value_user}\"";
$checkUserResult = mysqli_query($conn, $checkUser);
$checkUserResultRow = mysqli_fetch_array($checkUserResult);

if( isset( $_POST["usernameReg"]  ) ) 
{ 
    if($checkUserResultRow['username']!="")
    {
        echo "Username exists";
    }
    else
    {
        mysqli_query($conn, $addAcc);
        echo "Success";
    }
}



?>