PHP注册没有回应正确的结果

时间:2016-12-18 20:54:39

标签: php mysqli echo

<?php
$server_host = "host";
$server_username = "username";
$server_password = "password";
$server_dbName = "data base name";

$player_username = $_POST ["usernamePost"];
$player_password = $_POST ["passwordPost"];
$player_displayName = $_POST ["displayNamePost"];

$conn = new mysqli ($server_host, $server_username, $server_password, $server_dbName);

if (!$conn) {
    echo "Error connecting to the server.";
}

$query_code = "SELECT Username FROM users WHERE Username = '{$_POST[usernamePost]}'";
$result_login = mysqli_query ($conn,$query_code);
$anything_found = mysqli_num_rows ($result_login);

if ($anything_found > 0) {
echo "An account with this username or display name already exsists, please choose another.";  
}

if ($anything_found <= 0) {

$sql = "INSERT INTO users (Username, Password, Display_Name)
        VALUES ('".$player_username."','".$player_password."','".$player_displayName."')";
$result = mysqli_query ($conn,$sql);
if ($result) {
    echo "You may now login.";
}
if (!$result) {
        echo "Error.";




        }
}   
?>

我使用Unity Engine显示回显的结果,此脚本似乎会回显&#34;具有此用户名或显示名称的帐户已经存在,请选择另一个。&#34;如果已输入用户名的用户名?此外,它是否会回复#34;您现在可以登录。&#34;如果帐户已创建?

我自己编写了这个脚本,我是这个PHP的新手。如果有人看过这段代码并向我解释为什么这不起作用,我会很感激。

1 个答案:

答案 0 :(得分:-2)

逻辑错误。有条件的陈述危险地悬挂:-)
使用此

<?php
    $server_host = "host";
    $server_username = "username";
    $server_password = "password";
    $server_dbName = "data base name";

    $player_username = $_POST ["usernamePost"];
    $player_password = $_POST ["passwordPost"];
    $player_displayName = $_POST ["displayNamePost"];

    $conn = new mysqli ($server_host, $server_username, $server_password, $server_dbName);

    if (!$conn) {
        echo "Error connecting to the server.";
    }

    else{
    $query_code = "SELECT Username FROM users WHERE Username = '{$_POST[usernamePost]}'";
    $result_login = mysqli_query ($conn,$query_code);
    $anything_found = mysqli_num_rows ($result_login);

    if ($anything_found > 0) {
    echo "An account with this username or display name already exsists, please choose another.";  
    }

    elseif ($anything_found <= 0) {

    $sql = "INSERT INTO users (Username, Password, Display_Name)
            VALUES ('".$player_username."','".$player_password."','".$player_displayName."')";
    $result = mysqli_query ($conn,$sql);
    if ($result) {
        echo "You may now login.";
    }
    else{

        echo "Error.";

    }
    } 
    } 


 ?>