使用angular和php插入、显示和删除数据
我是Angular的新手,我正在努力学习Angular和php的教程。我已经完成了教程,现在正在尝试添加一个新的现场测试。我在数据库中添加了一个新字段。出于某种原因,这个新的输入字段不起作用,我想知道为什么。它显示第三个输入字段,但当我尝试添加一个具有第三个值的人时,它会作为null插入数据库中。这是我的密码 index.html使用angular和php插入、显示和删除数据,php,mysql,angularjs,Php,Mysql,Angularjs,我是Angular的新手,我正在努力学习Angular和php的教程。我已经完成了教程,现在正在尝试添加一个新的现场测试。我在数据库中添加了一个新字段。出于某种原因,这个新的输入字段不起作用,我想知道为什么。它显示第三个输入字段,但当我尝试添加一个具有第三个值的人时,它会作为null插入数据库中。这是我的密码 index.html <!doctype html> <html ng-app="ajaxExample"> <head lang="en">
<!doctype html>
<html ng-app="ajaxExample">
<head lang="en">
<meta charset="utf-8">
<title>Angular Ajax with PHP</title>
</head>
<body>
<h2>The form</h2>
<div ng-controller="mainController">
<form>
<p>Fill the form</p>
<div>
<label>Name</label>
<input type="text" ng-model="newName">
</div>
<div>
<label>Phone</label>
<input type="text" ng-model="newPhone">
</div>
<div>
<label>Tests</label>
<input type="text" ng-model="newTesting">
</div>
<input type="button" value="Add" ng-click="addPerson()">
</form>
<h2>The list</h2>
<p>The names that were added with the form.</p>
<ul>
<li ng-repeat="person in people">
<button ng-click="deletePerson( person.id )">Delete</button> {{ person.name }} {{ person.phone }} {{ person.testing }}
</li>
</ul>
</div>
<script src="/libs/angular.min.js"></script>
<script src="/assets/js/app.js"></script>
</body>
</html>
数据库
CREATE TABLE IF NOT EXISTS `people` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`testing` INT(5),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
php-类似于教程中的一个,数据库名为“test”
get.php
post.php
<?php
require 'connect.php';
$connect = connect();
// Add the new data to the database.
$postdata = file_get_contents("php://input");
var_dump($postdata);
if(isset($postdata) && !empty($postdata))
{
$request = json_decode($postdata);
$newName = preg_replace('/[^a-zA-Z ]/','',$request->newName);
$newPhone = preg_replace('/[^0-9 ]/','',$request->newPhone);
$newTesting = preg_replace('/[^0-9 ]/','',$request->newTesting);
if($newName == '' || $newPhone == '') return;
$sql = "INSERT INTO `people`(`name`, `phone`, `testing`) VALUES ('$newName','$newPhone', '$newTesting')";
mysqli_query($connect,$sql);
}
exit;
delete.php-与教程相同
感谢您的帮助检查拼写错误:
<input type="text" ng-model="newTesting">
data: {newName: $scope.newName, newPhone: $scope.newPhone, newTest: $scope.newTest }
型号和范围应具有相同的名称谢谢您的回答。我已经修复了它,但它仍然不起作用:试试$postdata=$\u POST;并删除$sql查询中的引号:姓名、电话、测试
<?php
require 'connect.php';
$connect = connect();
// Add the new data to the database.
$postdata = file_get_contents("php://input");
var_dump($postdata);
if(isset($postdata) && !empty($postdata))
{
$request = json_decode($postdata);
$newName = preg_replace('/[^a-zA-Z ]/','',$request->newName);
$newPhone = preg_replace('/[^0-9 ]/','',$request->newPhone);
$newTesting = preg_replace('/[^0-9 ]/','',$request->newTesting);
if($newName == '' || $newPhone == '') return;
$sql = "INSERT INTO `people`(`name`, `phone`, `testing`) VALUES ('$newName','$newPhone', '$newTesting')";
mysqli_query($connect,$sql);
}
exit;
<input type="text" ng-model="newTesting">
data: {newName: $scope.newName, newPhone: $scope.newPhone, newTest: $scope.newTest }