dougan5772 2015-12-08 07:36
浏览 57
已采纳

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

I am not able to connect to the database what should be the syntax when I have created the database (myDb) using phpMyAdmin. I have placed signup.php on the server i.e www folder. Please point out if there is some other mistake I have made in this code.

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条回答 默认 最新

  • donglvlao8367 2016-05-19 13:30
    关注

    use $request->email, $request->password, $request->username instead of $postdata->email, $postdata->password, etc...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算