如何将我的离子应用程序与MySQL数据库连接?

时间:2015-12-08 07:36:47

标签: php mysql rest ionic

当我使用phpMyAdmin创建数据库(myDb)时,我无法连接数据库的语法。我已将signup.php放在服务器上,即www文​​件夹。 请指出我在此代码中是否还有其他错误。

signup.html:

<ion-header-bar class="bar-positive">
<h2 class="title">SignUp</h2>
</ion-header-bar>

<ion-view view-title="SignUp" name="signup-view">
    <ion-content class="has-header">
    <div class="list list-inset">

   <label class="item item-input item-floating-label">
   <span class="input-label">Enter Username</span>
     <input class="form-control" type="text" ng-model="userdata.username" placeholder="Enter Username">
   </label>

   <label class="item item-input item-floating-label">
   <span class="input-label">Enter Your Email</span>
     <input type="text" ng-model="userdata.email" placeholder="Enter Your Email">
   </label>

   <label class="item item-input item-floating-label">
   <span class="input-label">Enter Your Password</span>
     <input class="form-control" type="password" ng-model="userdata.password" placeholder="Enter Your Password">
   </label>

   <button class="button button-block button-calm" ng-click="signUp(userdata)">SignUp</button><br>
   <span>{{responseMessage}}</span>
   </div>
   </ion-content>
 </ion-view>

signup.php:

<?php
    header("Content-Type: application/json; charset=UTF-8");
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');

    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $email = $postdata->email;
    $password = $postdata->password;
    $username = $postdata->username;

    $con = mysql_connect("localhost","root",'') or die ("Failed to connect to MySQL: " . mysql_error());;
    mysql_select_db('myDb', $con);

    $qry_em = 'select count(*) as cnt from users where email="' . $email . '"';
    $qry_res = mysql_query($qry_em);
    $res = mysql_fetch_assoc($qry_res);

    if($res['cnt']==0){
    $qry = 'INSERT INTO users (name,password,email) values ("' . $username . '","' . $password . '","' . $email . '")';
    $qry_res = mysql_query($qry);  
        if ($qry_res) {
            echo "1";
        } else {
            echo "2";;
        }
    }
    else
    {
        echo "0";
    }
?>

app.js:

.controller('SignupCtrl', function($scope, $http) {
    $scope.signup = function () {
    var request = $http({
        method: "post",
        url: "http://localhost/signup.php",
        crossDomain : true,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        data: {
            username: $scope.userdata.username,
            email: $scope.userdata.email,
            password: $scope.userdata.password
        },
    });
        request.success(function(data) {
        if(data == "1"){
         $scope.responseMessage = "Account Created Successfully!";
        }
        if(data == "2"){
         $scope.responseMessage = "Can not Create Account";
        }
        else if(data == "0") {
         $scope.responseMessage = "Email Already Exists"
        }  
    });
}
});

2 个答案:

答案 0 :(得分:3)

使用$request->email$request->password$request->username代替$postdata->email$postdata->password等...

答案 1 :(得分:0)

如果必须使用PHP,我建议您查看用于创建API的Slim Framework。其他一些适合此处的解决方案(可能比PHP和MySQL更好)Mongo + ExpressParseSDK for JavaScript也值得关注。我建议使用Parse,因为它很容易上手并消除很多后端头痛。

使用ionic访问API的示例示例:

<强>控制器:

app.controller('AppCtrl', function($scope){
    $http.get('API_URL')
        .then(
            function(data){
                console.log(data);
                $scope.data = data;
                // JSON data returned as response
            },
            function(err){
                console.log(err);
                $scope.err = err;
                // when error occurs
            }
        );
});

查看:

<ion-content ng-controller="AppCtrl">
    <div> {{ data }} {{ err }} </div>
</ion-content>

Example使用JSON数据。