douhuan1937 2018-08-02 08:15 采纳率: 0%
浏览 22

离子 - 尝试使用phpmyadmin登录

I am trying to make a login for my app with Ionic. So im sending data from ionic to PHP. Im not sure what im doing wrong.

HTML form part:

<ion-item id="roundedInput"> 
    <ion-label><ion-icon name="person" color="white"></ion-icon></ion-label>
    <ion-input placeholder="Gebruikersnaam" name="gebruikersnaam" #gebruikersnaam></ion-input>
</ion-item>

<ion-item id="roundedInput">
    <ion-label><ion-icon name="lock" color="white"></ion-icon></ion-label>
    <ion-input type="password" placeholder="Wachtwoord" name="wachtwoord" #wachtwoord> </ion-input>
</ion-item>

login.ts :

let data = {
    gebruikersnaam: this.gebruikersnaam.value,
    wachtwoord: this.wachtwoord.value
};

let loader = this.loading.create({
    content: 'Processing, please wait…',
});

loader.present().then(() => {
    this.http
        .post('https://rexmediahosting.nl/ionic/login.php', JSON.stringify(data))
        .map(response => response.json()).subscribe(response => {
            console.log(response)
            loader.dismiss()
            if (response =='success') {
                let alert = this.alertCtrl.create({
                    title: 'Succesvol ingelogt',
                    subTitle: (response),
                    buttons: ['OK']
                });
                alert.present();
            } else {
                let alert = this.alertCtrl.create({
                    title: 'error',
                    subTitle:(response),
                    buttons: ['OK']
                });
                alert.present()
            } 
        });
});

and the final part login.php: I have the login.php file "Acccess-Control-Allow-Origin" stuff, so CORS isn't the problem

// Create connection
$mysqli = new mysqli($localhost, $username, $password, $database);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$data = file_get_contents('php://input');
if (isset($data)){
    $request = json_decode($data);
    $gebruikersnaam = $request->gebruikersnaam;
    $wachtwoord = $request->wachtwoord;
    $gebruikersnaam = mysqli_real_escape_string($mysqli, $gebruikersnaam);
    $wachtwoord = mysqli_real_escape_string($mysqli, $wachtwoord);
    $gebruikersnaam = stripslashes($gebruikersnaam);
    $wachtwoord = stripslashes($wachtwoord);

    // if ($wachtwoord != ""){
    //     echo json_encode($wachtwoord);
    // } else {
    //     echo json_encode('no wachtwoord');
    // }

    // Maak Query aan
    $query = "SELECT * FROM Gebruikers WHERE gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";

    // Voer Query uit
    $result = mysqli_query($con, $query);
    // Controlleer of de login correct was 
    if (mysqli_num_rows($result) != 0 ){
        // login correct 
        $response = "succes";
    } else {
        // login incorrect 
        $response = "fail";
    }
    // Encode $response naar JSON
    echo json_encode($response);
}

image from the database structure : table name is 'Gebruikers' database structure

So basically, Im trying to make a login screen for my 'app'. for that i'm using phpmyadmin as db. So i try to pass data from ionic to an external php file on a server, to get a response. To check if my inputs are the same as in the database. When I put the correct values (same as in the database), I still get a 'fail' response. So it goes wrong somewhere, but I don't know where. I hope you guys can help me!!

  • 写回答

2条回答 默认 最新

  • dpblwmh5218 2018-08-02 09:04
    关注

    Problem is in your query:

    $query = "SELECT * FROM Gebruikers WHERE gebruikersnaam= $gebruikersnaam AND wachtwoord=$wachtwoord"; remove single quote from query as single quote does not evaluates value

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器