如何检查数据库中是否已存在电子邮件并将数据发送到UWP

时间:2017-09-02 06:32:00

标签: c# php mysql uwp

我使用登录和注册页面制作UWP应用程序

我使用PHP和MYSQL作为后端

但我想检查电子邮件是否存在以及是否存在,请向我的应用发送消息并在MessageDialog中显示<​​/ p>

这是我在UWP app中的代码:

 private async void Button_Click(object sender, RoutedEventArgs e)
    {
        Uri requestUri = new Uri("http://localhost/test/index.php");
        HttpClient client = new HttpClient();

        Dictionary<string, string> pairs = new Dictionary<string, string>();
        pairs.Add("email", emailbox.Text);
        pairs.Add("password", passwordbox.Text);

        HttpFormUrlEncodedContent encodedContent = new HttpFormUrlEncodedContent(pairs);

        await client.PostAsync(requestUri, encodedContent);     
    }

这是我的PHP代码:

<?php

    try
    {
        $bdd = new PDO("mysql:host=localhost;dbname=firstdb","root","");
    }
    catch(Exception $e)
    {
        die("ERROR".$e->getMessage());
    }

    $email = "";
    $req = "";
    $password = "";

    if(isset($_POST['email']) && isset($_POST['password']))
    {
        $email = $_POST['email'];
        $password = $_POST['password'];
        $req = $bdd->prepare("INSERT INTO users(email,password) VALUES (:email,:password)");
        $req->execute(array('email'=>$_POST['email'],'password'=>$_POST['password']));

    }

?>

将数据插入SQL数据库工作正常,但我不知道如何检查电子邮件是否已存在

1 个答案:

答案 0 :(得分:0)

使用选择查询检查电子邮件是否存在。如果存在则计算结果然后返回消息

if(isset($_POST['email']) && isset($_POST['password']))
{
    $email = $_POST['email'];
    $password = $_POST['password'];
    $stmt = $bdd->prepare("SELECT * FROM users WHERE email = :email");
    $stmt->execute(array('email'=>$email));
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    if(count($results)) //if record found
    {
      exit("Email already Exist");
    }

    $req = $bdd->prepare("INSERT INTO users(email,password) VALUES (:email,:password)");
    $req->execute(array('email'=>$_POST['email'],'password'=>$_POST['password']));

}