使用AngularJS将数据插入MySQL

时间:2019-06-16 12:32:07

标签: javascript php mysql angularjs crud

我写了一个简单的表格将数据插入MySQL数据库。表是员工(id,名称,电子邮件,公司名称,名称),其中id是int,主键和自动递增。表在数据库angularjs中。检查以下代码。错误在哪里?

testing.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Testing</title>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js" integrity="sha256-23hi0Ag650tclABdGCdMNSjxvikytyQ44vYGo9HyOrU=" crossorigin="anonymous"></script>
<script src="js/jquery.min.js"></script>

<div ng-app="insert" ng-controller="insertC">
<form>
    Name:<label>
    <input type="text" ng-model="name">
</label>
    Email:<label>
    <input type="email"  ng-model="email">
</label>
    Company Name:<label>
    <input type="text"  ng-model="companyName">
</label>
    Designation:<label>
    <input type="text"  ng-model="designation">
</label>
    <button ng-click="postData()">Submit</button>
</form>
</div>
</body>
</html>
<script>
    var app=angular.module("insert",[]);
    app.controller("insertC",function ($scope, $http) {
        $scope.postData=function () {
            $http({
                method:"post",
                url:'test.php',
                data:{
                    'name':$scope.name,
                    'email':$scope.email,
                    'companyName':$scope.companyName,
                    'designation':$scope.designation
                },
                headers:{'Content-Type':'application/x-www-form-urlencoded'}
            });
        }
    })
</script>

test.php

<?php

include('config.php');
$form_data=json_decode(file_get_contents("php://input"));

$query = "INSERT INTO angularjs.employee(name, email, companyName, designation) 
VALUES ($form_data->name,$form_data->email,$form_data->companyName,$form_data->designation)";
$stmt=$pdo->prepare($query);
$stmt->execute()

config.php

<?php

$DB_HOST='localhost';
$DB_NAME='angularjs';
$DB_USERNAME='root';
$DB_PASSWORD='';

$pdo=new PDO("mysql:host=localhost;dbname=angularjs",$DB_USERNAME,$DB_PASSWORD);

0 个答案:

没有答案