dsarttv037029 2017-02-22 19:25
浏览 408

检查Android代码中是否存在用户名

I am trying to check if the Text from my EditText is already in the Database. I am really new in PHP & MySQL.

I tried to receive all the Usernames and check if it equals to the EditText Text. At first it looked good. I checked it with

if(!UsernameDB.equals(editUsername.getText().toString()){
    Toast.makeText(Register.this, "Not taken", Toast.LENGTH_SHORT).show();
}

The Problem is, that it Toasts it for every Name. So i can't ask if everything is filled in right (Email, Password etc.) because it says its not matching after it passed the Name that is matching.

Does someone know how i can resolve my Problem?

  • 写回答

1条回答 默认 最新

  • douhui9192 2017-02-22 20:06
    关注

    Im not sure if you need to do it by Ajax or is it ok to refresh the site. Here is an example for refreshing: First of all you need a Formular (html):

    <?php include("loginFunktion.php"); ?>
    <form action="" method="Post">      
            <input name="name">
            <input type="submit" name="test" value="test">              
    </form>
    

    Thats the basic of your DB Class (new sheet):

    <?php 
    class DB {
    protected $mysqli;
    
    function __construct(){
        $this->host = "***";
        $this->username = "***";
        $this->database = "***";
        $this->password = "***";
    
    }
    
    function connect(){
        $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
    
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s<br/>", mysqli_connect_error());
            exit();
        }   
    }
    
    function disconnect() {
        // Verbindung beenden
        $this->mysqli->close();
    }
    }    
    ?>
    

    Now you have in include the class and connect to your DB by the constructor of your class (the following code is the loginFunktion.php sheet):

    <?php 
        include("dbclassname.php");
        $_DB = new DBData();
        $_DB -> connect();
    ?>
    

    Now you have a connection to your DB and u can do querys:

    if(isset($_POST["test"])){
    $queryCALL ="SELECT user FROM t_user WHERE user_name=".$_REQUEST["name"];
    $result = $this->mysqli->query($queryCALL);
    $ergebnis=$result->fetch_array()[0];
    if(count($ergebnis)>0)
        echo "this name is used";
    }
    

    If you are new on php ajax is a little bit to complicate for you. Nethertheless thats a good guide: https://www.w3schools.com/php/php_ajax_database.asp

    Next time please add more informations.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化