控制器出错,无法用php api将数据表单发送到mysql

时间:2016-12-19 15:20:51

标签: php mysql angularjs cordova ionic-framework

我想使用PHP API将2个表单中的数据插入到我的数据库中。但是我收到了一个错误。请帮我解决这个问题。

我的表单:testing_form.html

<ion-view view-title="Form Insert">
<ion-content>
    <div class="list">
        <div ng-app="myApp" ng-controller="cobaInsert" >
          <label class="item item-input">
            <input type="text" placeholder="Username" ng-model="username">
          </label>
          <label class="item item-input">
            <input type="password" placeholder="Password" ng-model="password">
          </label>
          <button class="button button-block button-positive" ng-click="insertData()">
              Submit
            </button>
        </div>
    </div>
</ion-content>

我的控制器:controller.js

angular.module('myApp',[]);
.controller('cobaInsert',function($scope, $stateParams, $http){    
        $scope.insertData=function(){      
            $http.post('http://pvcmate.com/api/coba_form.php', {
                'username':$scope.username,
                'password':$scope.password
            }).then(function(response){
                    console.log("Data Inserted Successfully");
                },function(error){
                    alert("Sorry! Data Couldn't be inserted!");
                    console.error(error);
                });
            }
        });

这是我的php文件:coba_form.php

<?php 
$data = json_decode(file_get_contents("php://input"));
// $bname = mysql_real_escape_string($data->bname);
// $bauthor = mysql_real_escape_string($data->bphone);

$username = $data->username;
$password = $data->password;


mysql_connect("localhost", "root", "password"); 
mysql_select_db("testing");

mysql_query("INSERT INTO 'coba_form'('','username','password')
    VALUES ('".$username."','".$password."') ") or die(mysql_error());
//echo $bname." ".$bphone;
?>

2 个答案:

答案 0 :(得分:0)

您没有将模型添加到ng-click="insertData()"

usernamepassword添加到ng-click,例如ng-click="insertData(username,password)"

或制作类似ng-model="something.username"ng-model="something.password"的模型,并将something添加到ng-click,例如ng-click="insertData(something)"

P.S。使用mysqli代替mysql。 - More about this

答案 1 :(得分:0)

这是我的PHP

$entity->get('field_image')->entity->getFileUri();
$entity->get('field_image')->entity->url();

有什么问题?感谢