插入操作不适用于Angular JS

时间:2019-05-30 07:08:06

标签: php sql angularjs

我正在尝试使用Angular JS和mysql执行插入操作,但是它正在工作。请检查以下代码,并帮助我找出问题所在。

以下是我的index.html文件的代码

<html ng-app="myapp">
   <head>
      <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
      <script src="js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
   </head>
   <body ng-controller="formctrl" novalidate>
      <form ng-submit="submitStudentForm()">
         <input type="text" name="fname" ng-model="fname" placeholder="First Name"><br>
         <input type="text" name="lname" ng-model="lname" placeholder="Last Name"><br>
         <input type="submit" value="Submit" name="submit"/>
      </form>
      <script>
         var app = angular.module('myapp', []);
         app.controller('formctrl', function ($scope, $http) {
                 $scope.submitStudentForm = function () {
                     $http.post("add.php");
                 };
             }
         );

      </script>
   </body>
</html>

以下是add.php的代码

<?php
$conn = new mysqli("localhost", "root", "root", "test2");
if (isset($_POST['submit'])) {
    $submit = $_POST['submit'];
    $fname  = $_POST['fname'];
    $lname  = $_POST['lname'];
    $sql    = "INSERT into info(fname, lname)
    VALUES ('$fname', '$lname')";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

我敢打赌您没有要发布在您的http帖子上的数据。尝试在您的http帖子上进行编辑:

$http.post("add.php", {
'fname':$scope.fname,
'lname':$scope.lname})