PHP表单详细信息不存储在mysql中

时间:2017-05-31 09:08:15

标签: php html mysql angularjs

我正在AngularJS中创建一个表单,我必须在phpMyAdmin(mysql)中存储人员详细信息。但是只要我在填写表单后点击提交按钮,详细信息就不会添加到数据库中,任何人都可以帮我解决这个问题。

HTML网页代码:

<!DOCTYPE html>
<html>
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<title> Home </title>
<body>

<div ng-app="myapp" ng-controller="myCtrl">
<form>
<br>
<h1 align="left">ID : <input type="text" ng-model="id"></h1>
<h1 align="left">Name : <input type="text" ng-model="name"></h1>

<h1 align="left">Address : <input type="text" ng-model="address"></h1>

<h1 align="left">Phone : <input type="text" ng-model="phone"></h1>

<input type="button" value="Submit" ng-click="insertData()" />
</br>
</form>

<script>
var app = angular.module('myapp',[]);
app.controller('myCtrl', function($scope, $http){

$scope.insertData = function(){
$http.post("insert.php",{
'id':$scope.id, 
'name':$scope.name, 
'address':$scope.address, 
'phone':$scope.phone})
.then(function successCallback(data,status,headers,config) {
console.log(data);
});
}
});

PHP代码:insert.php

 <?php
 $servername = "localhost";
 $username   = "root";
 $password   = "bulbul";
 $dbname     = "angularjs";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 }


 $json = file_get_contents('php://input');
 $php_array = json_decode($json, TRUE);
 $id = $php_array['id'];
 $name = $php_array['name'];
 $address = $php_array['address'];
 $phone = $php_array['phone'];

 $sql = "INSERT INTO employee(id,name,address,phone) VALUES(?,?,?,?)";
 $stmt = $conn->prepare($sql);
 $stmt->bind_param("isss", $id, $name, $address, $phone);
 if ($stmt->execute()) {

  echo "data inserted";
 }
 ?>

2 个答案:

答案 0 :(得分:0)

使用.then而不是.success 因为.success在最新的角度1.x版本中已弃用

http://www.codelord.net/2015/05/25/dont-use-%24https-success/

在php代码中使用die检查任何服务器端问题

答案 1 :(得分:0)

为什么没有在数据库中添加表单详细信息?因为您有以下错误:

  

$ http.post(...)。成功不是一个功能

使用浏览器控制台。

问题是.success $ http成功和错误方法在旧版Angular 1.x中可用,但它们已被弃用了一段时间并在Angular 1.6中被删除。见:https://docs.angularjs.org/api/ng/service/

您应该使用.then()

这就是你需要的

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<title> Home </title>
<body>

<div ng-app="myapp" ng-controller="myCtrl">
<form>
<br>
<h1 align="left">ID : <input type="text" ng-model="id"></h1>
<h1 align="left">Name : <input type="text" ng-model="name"></h1>

<h1 align="left">Address : <input type="text" ng-model="address"></h1>

<h1 align="left">Phone : <input type="text" ng-model="phone"></h1>

<input type="button" value="Submit" ng-click="insertData()" />
</br>
</form>

<script>
var app = angular.module('myapp',[]);
app.controller('myCtrl', function($scope, $http){

    $scope.insertData = function(){
    $http.post("insert.php",{
    'id':$scope.id, 
    'name':$scope.name, 
    'address':$scope.address, 
    'phone':$scope.phone})
    .then(function successCallback(data,status,headers,config) {
    console.log(data);
    });
    }
    });
    </script>

    </div>
    </body>
    </html>

然后你的insert.php

停止使用折旧函数并停止使用危险代码使用预准备语句:

<?php
$servername = "localhost";
$username   = "root";
$password   = "";
$dbname     = "angularjs";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$data = json_decode(file_get_contents("php://input"));
$id = $data->id;
$name = $data->name;
$address = $data->address;
$phone = $data->phone;

$sql = "INSERT INTO employee(id,name,address,phone) VALUES(?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("isss", $id, $name, $address, $phone);
if ($stmt->execute()) {

    echo "data inserted";
}
?>