角度模式表单-使用PHP和Mysql插入数据

时间:2018-12-06 09:56:09

标签: php mysql angularjs angular-schema-form

我将使用角度模式表单将数据插入数据库,但是如何将schemaForm数据发布到PHP中,我的代码有什么问题,请让我知道如何了解模式表单文档。

app.js

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

app.controller('MainCtrl', function($scope,$http) {

  $scope.schema = {
    "type": "object",
    "title": "Comment",
    "properties": {
      "name": {
        "title": "Name",
        "type": "string"
      }
    },
};

  $scope.form = 
  [
    "name",
    {
      "type": "submit",
      "style": "btn-info",
      "title": "Submit",
      "onclick":insert()
    }
  ];


  $scope.model = {};


  $scope.insert=function()
  {
    $http.post("insert.php",
    {
      'name' :$scope.schema[name]
    }).then(function(response)
    {
      console.log("Data Inserted Successfully");
    },
    function(error)
    {
      alert("Sorry! Data Couldn't be inserted!");
      console.error(error);
    });
  }

});

这是我进行数据库连接的PHP代码,

<?php
$conn = mysqli_connect("localhost", "root", "demo", "angular");
$info = json_decode(file_get_contents("php://input"));
echo $info->name;
if (count($info) > 0) 
{
    $name     = mysqli_real_escape_string($conn, $info->name);
    $query = "INSERT INTO crud(name) VALUES ('$name')";
    if (mysqli_query($conn, $query)) 
    {
        echo "Data Inserted Successfully...";
    } 
    else {
        echo 'Failed';
    }
}
?>

请帮助

0 个答案:

没有答案