简体   繁体   English

通过php将数据发布到AngularjS中的数据库

[英]Post Data via php to my Database in AngularjS

I am completly new to AngularJs but I am trying to learn it, since it seems pretty cool. 我对AngularJs完全陌生,但是我尝试学习它,因为它看起来很酷。 However, I am trying to put data from AngularJS into my database, for this,I read some tutorials and tried the following: 但是,我试图将AngularJS中的数据放入数据库中,为此,我阅读了一些教程并尝试了以下方法:

ctrl.js ctrl.js

var app = angular.module('controller', []);

app.controller('controleCtrl', function($scope, $http){
    "use strict";
    $scope.value = 0;
    $scope.parcel = 1;

postData();

function postData(){
        $http({
            method: 'POST',
            url: 'http://192.168.1.35/updateModus.php',
            data: {
                'value': $scope.value,
                'parcel': $scope.parcel
            }
        })
            .then(function(response){
                console.log("Success");
                //success
        },
            function(response){
                console.log("failed");
                //failed
            })
        ;
    }

updateModus.php updateModus.php

<?php
$servername = 172.0.0.1
$username = "root";
$password = "";
$dbname = "uebergabestation";

$value = $_POST['value'];
$parcel = $_POST['parcel'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO betriebsmodus (Modus, Parzelle) VALUES (" + $value + ", " + $parcel +")";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

The console is telling me every time "success", even with different methods, but the DB does not get additional data. 控制台每次都会告诉我“成功”,即使使用不同的方法也是如此,但是DB不会获取其他数据。 Can anyone tell me whats wrong? 谁能告诉我怎么了? Why does it say succes but not add the data to the database? 为什么说成功但不将数据添加到数据库?

Thanks in advance 提前致谢

as @gaurav mentioned, there is a error in sql insert statement 如@gaurav所述,sql insert语句中有错误

$sql = "INSERT INTO betriebsmodus (Modus, Parzelle) VALUES ('" . $value . "', '". $parcel ."')"; $ sql =“ INSERT INTO betriebsmodus(Modus,Parzelle)VALUES('”。$ value。“','”。$ parcel。“'))”;

In php, you need to use dot concatenation operation and the single quotes for a string value insertion into DB. 在php中,您需要使用点连接操作和单引号将字符串值插入数据库。

It keeps on printing success because in console.log you always prints success which is hardcoded . 它保持打印成功,因为在console.log中,您始终打印已硬编码的成功。 Instead print console.log(response); 而是打印console.log(response); So you will get to know what happened on backend ajax call. 因此,您将了解后端ajax调用发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM