2015-12-08 62 views
1

我無法連接到數據庫當我使用phpMyAdmin創建數據庫(myDb)時,應該使用哪種語法。我已經在服務器上放置了signup.php,即www文​​件夾。 請指出我在這段代碼中是否有其他錯誤。如何將我的離子應用程序連接到MySQL數據庫?

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" 
     } 
    }); 
} 
}); 
+1

那麼你的問題是什麼? – Vineet1982

+0

我需要知道如何使MySQL與我的離子應用程序交談的過程。 –

+0

我不明白爲什麼人際關係會投票回答我的問題...... !!! –

回答

2

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

0

如果PHP是必須的,我會建議看Slim Framework這是用於創建API。還有一些適合這裏的解決方案(可能比PHP更好)MySQL爲此目的)是Mongo + ExpressParseSDK for JavaScript也是要看的東西。我會推薦Parse,因爲它很容易開始使用並消除很多後端頭痛。使用離子訪問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> 

JSON數據的使用的Example

相關問題