jQuery脚本没有在数据库Mysql中写入值

时间:2017-02-14 12:06:35

标签: javascript jquery angularjs html5

我的数据库有一个名为utilizatori的表,并且有id,username,nume_prenume,parola,locatie,sambata列。

通过表单,我试图在数据库中添加新条目,但是当我提交表单时,它不会添加任何条目。 可以找到演示HERE。 更新和删除操作正常。

HTML表单:

<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脚本:

// 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');
   }
}]);

插入信息PHP脚本:

<?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;
?>

和database_connections.php脚本:

<?php
$con = mysqli_connect("localhost", "user", "pasword", "database");
?>

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。我试着用4列写,但是有6列(不包括自动增量的id。

如果我重写此查询以全部写入6,或者我从数据库中删除其他2列,则可以正常工作。

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