无法从我的离子应用程序将数据发布到MySql数据库

时间:2016-02-24 11:27:48

标签: php mysql angularjs rest ionic-framework

我无法将数据发布到MySql数据库。我在chrome浏览器(windows7)上运行该项目。在这里,我可以看到参数,但它们不会被发送到数据库表。我的代码实际上有什么问题?

我的PHP代码是:

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');

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


    $con = mysqli_connect("localhost","root",'') or die ("Could not connect: " . mysql_error());;
    mysqli_select_db($con, 'db_lastLog');

    $qry_em = 'select count(*) as cnt from users where email ="' . $email . '"';
    $qry_res = mysqli_query($con, $qry_em);
    $res = mysqli_fetch_assoc($qry_res);

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

我的控制器代码是:

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

我的HTML代码是:

<ion-pane>
<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" ng-controller="SignupCtrl">
    <div class="list list-inset">

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

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

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

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

0 个答案:

没有答案