Max R. Max R. - 1 month ago 7
AngularJS Question

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. However, I am trying to put data from AngularJS into my database, for this,I read some tutorials and tried the following:

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

<?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. Can anyone tell me whats wrong? Why does it say succes but not add the data to the database?

Thanks in advance

Answer

as @gaurav mentioned, there is a error in sql insert statement

$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.

It keeps on printing success because in console.log you always prints success which is hardcoded . Instead print console.log(response); So you will get to know what happened on backend ajax call.