doqo89924 2016-03-29 07:57
浏览 48

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

I've been working on a way to push variables from my application to my database. It now succesfully makes a connection, but it doesn't push the variables into the database (It makes a new row, but the the variables are empty.)

I'll post my 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")
})

My example.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;

?>

I suspect there is an error in how example.php handles the delivered variables, but I am not sure.

Thanks in advance! :)

EDIT:

Error message for clarification

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

SECOND EDIT:

Print_r($_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"}] => ) 
  • 写回答

2条回答 默认 最新

  • douwen0612 2016-03-29 08:17
    关注

    Try this with PDO prepared statements instead, much safer, prevents SQL injection etc.

    $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);
    


    In angular try: (taken from the 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.
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值