Php检查是否存在帐号

时间:2015-01-11 18:56:52

标签: php database login 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/>";
    }
    }

?>

4 个答案:

答案 0 :(得分:1)

可能有两个原因 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/>";
      }

}

答案 1 :(得分:1)

  

你能编写代码吗? - MisterTudi

应该是这样的。 使用预备语句,因为 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.';
}

答案 2 :(得分:1)

根据您的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";}

答案 3 :(得分:1)

以下是您的代码的工作示例

<?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";
    }
}



?>