Max R. Max R. - 1 year ago 63
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:


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

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


function postData(){
method: 'POST',
url: '',
data: {
'value': $scope.value,
'parcel': $scope.parcel


$servername =
$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;


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download