Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My database has a table called utilizatori and has id,username,nume_prenume,parola,locatie,sambata columns.

Through the form, I am trying to add new entries in database but when I submit the form, it doesn`t add any entry. A demo can be found HERE. The update and delete operations are working fine.

HTML form:

<script src="js/jQuery/jquery.min.js"></script>
<!-- Include AngularJS library -->
<script src="lib/angular/angular.min.js"></script>
<!-- Include Bootstrap Javascript -->
<script src="js/bootstrap.min.js"></script>
<form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo);" hidden>
   <h3 class="text-center">Insert Employee Details Into Database</h3>
   <div class="form-group">
      <label for="Username">Nume utilizator:</label>
      <input type="text" name="username" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.username" value="" autofocus required />
   </div>
   <div class="form-group">
      <p class="text-danger" ng-show="empList.username.$invalid && empList.username.$dirty">Name field is Empty!</p>
   </div>
   <div class="form-group">
      <label for="Nume_prenume">Nume angajat:</label>
      <input type="text" name="nume_prenume" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.nume_prenume" autofocus required />
   </div>
   <div class="form-group">
      <p class="text-danger" ng-show="empList.nume_prenume.$invalid && empList.nume_prenume.$dirty">Invalid Email!</p>
   </div>
   <div class="form-group">
      <label for="Parola">Parola cont:</label>
      <input type="text" name="parola" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.parola" value="" autofocus required />
   </div>
   <div class="form-group">
      <p class="text-danger" ng-show="empList.parola.$invalid && empList.parola.$dirty">Name field is Empty!</p>
   </div>
   <div class="form-group">
      <label for="Rol_user">Rol:</label>
      <input type="text" name="rol_user" class="form-control" placeholder="Enter Employee Address" ng-model="empInfo.rol_user" autofocus required />
   </div>
   <div class="form-group">
      <p class="text-danger" ng-show="empList.rol_user.$invalid && empList.rol_user.$dirty">Address field is Empty!</p>
   </div>
   <div class="form-group">
      <button class="btn btn-warning" ng-disabled="empList.$invalid">Add Into Database</button>
   </div>
</form>
<script src="js/angular-script.js"></script>

Angular JS script:

// Application module
var crudApp = angular.module('crudApp', []);
crudApp.controller("DbController", ['$scope', '$http', function($scope, $http) {

   // Function to get employee details from the database
   getInfo();

   function getInfo() {
      // Sending request to EmpDetails.php files 
      $http.post('databaseFiles/empDetails.php').success(function(data) {
         // Stored the returned data into scope 
         $scope.details = data;
      });
   }

   // Setting default value of gender 
   $scope.empInfo = {
      'rol_user': 'Operator receptie'
   };
   // Enabling show_form variable to enable Add employee button
   $scope.show_form = true;
   // Function to add toggle behaviour to form
   $scope.formToggle = function() {
      $("#empForm").slideToggle();
      $("#editForm").css('display', 'none');
   }
   $scope.insertInfo = function(info) {
      $http.post('databaseFiles/insertDetails.php', {
         "username": info.username,
         "nume_prenume": info.nume_prenume,
         "parola": info.parola,
         "rol_user": info.rol_user
      }).success(function(data) {
         if (data == true) {
            getInfo();
            $("#empForm").css('display', 'none');
         }
      });
   }
   $scope.deleteInfo = function(info) {
      $http.post('databaseFiles/deleteDetails.php', {
         "del_id": info.id
      }).success(function(data) {
         if (data == true) {
            getInfo();
         }
      });
   }
   $scope.currentUser = {};
   $scope.editInfo = function(info) {
      $scope.currentUser = info;
      $('#empForm').slideUp();
      $('#editForm').slideToggle();
   }
   $scope.UpdateInfo = function(info) {
      $http.post('databaseFiles/updateDetails.php', {
         "id": info.id,
         "username": info.username,
         "nume_prenume": info.nume_prenume,
         "parola": info.parola,
         "rol_user": info.rol_user
      }).success(function(data) {
         $scope.show_form = true;
         if (data == true) {
            getInfo();
         }
      });
   }
   $scope.updateMsg = function(emp_id) {
      $('#editForm').css('display', 'none');
   }
}]);

Insert info PHP script:

<?php 
    // Including database connections
    require_once 'database_connections.php';
    // Fetching and decoding the inserted data
    $data = json_decode(file_get_contents("php://input")); 
    // Escaping special characters from submitting data & storing in new variables.
    $username = mysqli_real_escape_string($con, $data->username);
    $nume_prenume = mysqli_real_escape_string($con, $data->nume_prenume);
    $parola = mysqli_real_escape_string($con, $data->parola);
    $rol_user = mysqli_real_escape_string($con, $data->rol_user);

    // mysqli insert query
    $query = "INSERT into utilizatori (username,nume_prenume,parola,rol_user) VALUES ('$username','$nume_prenume','$parola','$rol_user')";
    // Inserting data into database
    mysqli_query($con, $query);
    echo true;
?>

And database_connections.php script:

<?php
$con = mysqli_connect("localhost", "user", "pasword", "database");
?>
share|improve this question
    
id is AUTO_INCREMENT PRIMARY key. My server is WAMP. – Bogdan2305 2 days ago
    
update and delete doesn't work for me in that demo, instead I can see error messages in the console. – Connum 2 days ago
    
Does $http.post actually send JSON? – CBroe 2 days ago
    
In that demo update and delete are most likely disabled. It is a demo but if you install it on your server, will work. – Bogdan2305 2 days ago
    
Why downvote? I do not understand. – Bogdan2305 2 days ago

I found the issue. I was trying to write in 4 columns, but there are 6 (excluding id which is auto increment.

If I rewrite this query to write in all 6 or I remove the other 2 columns from database, will work.

// mysqli insert query
    $query = "INSERT into utilizatori (username,nume_prenume,parola,rol_user) VALUES ('$username','$nume_prenume','$parola','$rol_user')";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.