我正在尝试使用angular和php将数据插入数据库中,但是我的代码无法正常工作。任何人都可以检查并帮助我找出问题所在。在此先感谢
请参见下面的test.html文件的代码:
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myapp" data-ng-controller="crud">
<table>
<form name="empList" id="empform" ng-submit="insertInfo(empInfo);" hidden>
<tr>
<td>Name :</td>
<td><input type="text" name="name" ng-model="empInfo.name"></td>
</tr>
<tr>
<td>Email :</td>
<td><input type="email" name="email" ng-model="empInfo.email"></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="number" name="phone" ng-model="empInfo.phone"></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" ng-model="empInfo.address"></td>
</tr>
<tr>
<td><button class="btn btn-warning" ng-disabled="empList.$invalid">Add Into Database</button></td>
</tr>
</form>
</table>
</div>
<script>
var app=angular.module(myapp,[]);
app.controller('crud', function(scope)
$scope.insertInfo = function(info){
$http.post('add.php',{"name":info.name,"email":info.email,"phone":info.phone,"address":info.address}).success(function(data){
if (data == true) {
Success
}
});
}
);
</script>
请查看我的php文件的代码
<?php
require_once 'conn.php';
$data=json_decode(file_get_contents("php://input"));
$name=mysqli_real_escape_string($conn,$data->name);
$email=mysqli_real_escape_string($conn,$data->email);
$phone=mysqli_real_escape_string($conn,$data->phone);
$address=mysqli_real_escape_string($conn,$data->address);
$query="INSERT into student(name, email, phone, address)
VALUES('$name', '$email', '$phone', 'address')";
$result=mysqli_query($conn,$query);
if($result){
echo "Data Inserted Successfully";
}
?>