无法使用Ionic将数据发布到数据库

时间:2016-04-16 13:44:24

标签: php angularjs post service ionic-framework

我正在努力获得创建Ionic应用程序的一些经验。在我的应用程序中,我创建了一个注册表单。

register.html

<form ua-signup ng-controller="registerCtrl">
  <ion-view title="Sign Up">
    <ion-content>
      <div class="list list-inset">
        <label class="item item-input">
          <span class="input-label">ID</span>
          <input type="text" ng-model="data.id">
        </label>
        <label class="item item-input">
          <span class="input-label">First Name</span>
          <input type="text" ng-model="data.fname">
        </label>
        <label class="item item-input">
          <span class="input-label">Last Name</span>
          <input type="text" ng-model="data.lname">
        </label>
        <label class="item item-input">
          <span class="input-label">Email</span>
          <input type="text" ng-model="data.email" ua-is-email>
        </label>
        <label class="item item-input">
          <span class="input-label">Password</span>
          <input type="password" ng-model="data.pass">
        </label>
        <label class="item item-input">
          <span class="input-label">Mobile No.</span>
          <input type="tel" ng-model="data.tel">
        </label>
        <label class="item item-input item-select">
          <div class="input-label" ng-model="data.course">
            Course
          </div>
          <select>
            <option selected>HUBIT</option>
            <option>HUBMC</option>
            <option>HUBIM</option>
            <option>HUBGM</option>
          </select>
        </label>
         <label class="item item-input">
          <div class="input-label">
            Subject
          </div>
          <select multiple="multiple">
            <option selected>HUBIT</option>
            <option>HUBMC</option>
            <option>HUBIM</option>
            <option>HUBGM</option>
          </select>
        </label>
      </div>
      <div class="padding">
        <button type="submit" class="button button-block button-positive" ng-click="Register(data);">
          <span ng-show="!loading">Create Account</span>
          <i ng-show="loading" class="ion-loading-b"></i>
        </button>
      </div>
    </ion-content>
  </ion-view>
  <div class="bar bar-footer bar-assertive" ng-show="error">
  </div>
</form>

controller.js

app.controller('registerCtrl', function($scope,$http,$state) {
        $scope.user = {};

        $scope.register = function(data){
          $scope.id = $scope.data.id;
          $scope.fname = $scope.data.fname;
          $scope.lname = $scope.data.lname;
          $scope.email = $scope.data.email;
          $scope.pass = $scope.data.pass;
          $scope.tel = $scope.data.tel;
          $scope.course = $scope.data.course;
          $http.post("http://localhost:81/fyp/register.php",{
            'id':$scope.id,
            'fname':$scope.fname,
            'lname':$scope.lname,
            'email':$scope.email,
            'pass':$scope.pass,
            'tel':$scope.tel,
            'course':$scope.course
          })
          .success(function(data, status, headers, config){
            console.log("data inserted");
          })
           $state.go('tab.dash');
          }
});

我的 register.php 位于xampp服务器上

?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }



$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$id = mysql_real_escape_string($request->id);
$fname = mysql_real_escape_string($request->fname);
$lname = mysql_real_escape_string($request->lname);
$password = mysql_real_escape_string($request->pass);
$email = mysql_real_escape_string($request->email);
$phone = mysql_real_escape_string($request->tel);
$course = mysql_real_escape_string($request->course);

// Connect to MySQL
$db = mysql_connect("localhost","root",""); 
if (!$db) {
die("Could not connect to database </body></html>");
};

// open a9963146_fyp16 database
$db_select = mysql_select_db('fyp',$db);
if (!$db_select) {
die("Could not connect to database </body></html>");
};  


if($id != '' && $lname != '' & $password != '' && $email != '' && $phone != ''){
    $query = mysql_query("INSERT into Student(id, firstname, lastname, password, email, phone, course) values ('$id', '$fname', '$lname', '$password', '$email', '$phone', '$course')");
    echo "Registered successfully!";
}
else{
    echo "All fields must be filled!";
}

mysql_close( $db );
?>      

我还是新手,我正在练习。但我发现我试图插入的值没有添加到我的数据库中。为什么会这样?我在controller.js和register.php中的代码是否有问题?有人能告诉我我犯的错误,我该如何纠正?

1 个答案:

答案 0 :(得分:0)

我建议您首先将$http.post更改为以下内容:

var body = {
    'id':$scope.id,
    'fname':$scope.fname,
    'lname':$scope.lname,
    'email':$scope.email,
    'pass':$scope.pass,
    'tel':$scope.tel,
    'course':$scope.course
};

$http.post("http://localhost:81/fyp/register.php", body)
.then(function(response) {
  console.log(response);
}, function(error) {
  console.log(error);
}

原因是此函数已弃用.success方法,如here所述:

  

$ http遗留承诺方法成功与错误   弃用。请改用标准方法。如果   $ httpProvider.useLegacyPromiseExtensions设置为false然后这些   方法将抛出$ http / legacy错误。

这可能无法解决您的问题,但添加的错误功能应该可以让您查看是否有任何错误,然后您可以与我们分享,以便我们为您提供进一步的帮助。增加的身体变量是完全可选的。