Angular-PHP API没有变量插入到数据库中


Angular - PHP API No variables inserted in to database

我一直在研究将变量从应用程序推送到数据库的方法。它现在成功地建立了一个连接,但它没有将变量推入数据库(它创建了一个新行,但变量是空的。)

我将发布我的http.get

$scope.signupuser = function(info){
$scope.user = [];
$scope.user.email = 
$scope.user.voornaam = 
$scope.user.achternaam = 
$scope.user.postcode = 
$scope.user.woonplaats = 
$scope.request = $http({
    url: "www.test.com/example.php",
    method: "POST",
    params: { 
        voornaam: $scope.user.voornaam, 
        achternaam: $scope.user.achternaam, 
        email: $scope.user.email, 
        postcode: $scope.user.postcode, 
        woonplaats: $scope.user.woonplaats
    },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
$scope.request.success(function(data, status, headers, config){
    console.log("inserted Successfully")
})

我的示例.php

<?php
include '../dbconnect.php';
try {
     json_decode(file_get_contents("php://input"));
     print_r($_POST);
     // set the PDO error mode to exception
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $sql = "INSERT INTO 
                users ( 
                     email, 
                     voornaam, 
                     achternaam, 
                     postcode, 
                     woonplaats
                ) VALUES (
                     '$email', 
                     '$voornaam', 
                     '$achternaam', 
                     '$postcode', 
                     '$woonplaats'
                )";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

我怀疑example.php处理传递的变量的方式有错误,但我不确定。

提前感谢!:)

编辑:

澄清的错误消息

Array ( [{"voornaam":"gfa","achternaam":"g","email":"JohnDoe@gmail.com","postcode":"8271AT","woonplaats":"fdsa"}] => ) 
Notice: Undefined index: email in /var/www/vhosts/text.com/httpdocs/example.phpon line 13
Notice: Undefined index: voornaam in /var/www/vhosts/text.com/httpdocs/example.php on line 14
Notice: Undefined index: achternaam in /var/www/vhosts/text.com/httpdocs/example.php on line 15
Notice: Undefined index: postcode in /var/www/vhosts/text.com/httpdocs/example.php on line 16
Notice: Undefined index: woonplaats in /var/www/vhosts/text.com/httpdocs/example.php on line 17
New record created successfully

第二次编辑:

打印($_POST)

Array ( [{"voornaam":"Jogn","achternaam":"Doe","email":"johndoe@gmail_com","postcode":"3252AR","woonplaats":"Zwolle"}] => ) 

print_r($_POST[0])

   Notice: Undefined offset: 0 in /var/www/vhosts/test.com/httpdocs/example.php on line 9

print_r($_REQUEST)

Array ( [{"voornaam":"Jogn","achternaam":"Doe","email":"johndoe@gmail_com","postcode":"3252AR","woonplaats":"Zwolle"}] => ) 

尝试对数据使用serialzier,我更喜欢使用$httpParamSerializerJQLike。不要忘记将其添加到控制器依赖项中,您的代码应该是

var data={ 
    voornaam: $scope.user.voornaam, 
    achternaam: $scope.user.achternaam, 
    email: $scope.user.email, 
    postcode: $scope.user.postcode, 
    woonplaats: $scope.user.woonplaats
}
$scope.request = $http({
url: "www.test.com/example.php",
method: "POST",
data: $httpParamSerializerJQLike(data),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

尝试使用PDO准备的语句,这样更安全,可以防止SQL注入等。

$sql = "INSERT INTO 
            users ( 
                 email, 
                 voornaam, 
                 achternaam, 
                 postcode, 
                 woonplaats
            ) VALUES (
                 ':email', 
                 ':voornaam', 
                 ':achternaam', 
                 ':postcode', 
                 ':woonplaats'
            )";
$params = array(
    ':email'   => $_POST['email'],
    ':voornam' => $_POST['voornaam']
    // etc
)
$statement = $conn->prepare($sql);
$result = $statement->execute($params);


在angular尝试中:(取自angular Docs)

var params = { 
    voornaam: $scope.user.voornaam, 
    achternaam: $scope.user.achternaam, 
    email: $scope.user.email, 
    postcode: $scope.user.postcode, 
    woonplaats: $scope.user.woonplaats
}
$http
    .post("www.test.com/example.php", params)
    .then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        console.log("inserted Successfully")
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });
});