数据没有使用angularjs中的php插入数据库

时间:2017-11-29 11:11:18

标签: php mysql angularjs

<!DOCTYPE html>
<html ng-controller="controller" ng-app="app">
    username:<input type="text" placeholder="*username"  ng-model="user"><br><BR>
    password:<input type="password" placeholder="*password" ng-model="pass" ><br><BR>
Email:&emsp;<input type="email" placeholder="*email"  ng-model="email"><br><BR>
<div align="center">
<input type="submit" value="register" ng-click="insert()">
<input type="button" value="Back" >
    </div>


    <script>
var app = angular.module("app", []);
app.controller("controller", function($scope, $http) {
    $scope.insert = function() {
        $http.post(
            "insert.php", {
                'user':$scope.user,
                'pass':$scope.pass,
                'email':$scope.email
            }).then(function(response){
                    console.log("Data Inserted Successfully");
                },function(error){
                    alert("Sorry! Data Couldn't be inserted!");
                    console.error(error);

                });
            }
        });
</script>
    </html>

.PHP

<?php
$conn = mysqli_connect("localhost", "root", "root", "students");
$info = json_decode(file_get_contents("php://input"),true);
if(count($info) > 0) {

            $user = $data->user;
            $pass = $data->pass;
            $email=$data->email;

    $query = "INSERT INTO students(username, password, email) VALUES('".$user."','".$pass."','".$email."')";

    if(mysqli_query($conn, $query)) {
        echo "Insert Data Successfully";
    }
    else {
        echo "Failed";
    }
}
?>

数据没有插入到表中,控制台上没有错误...代码中的错误..通过许多视频但无法找到它...我正在使用mysql workbench 6.3 v..anyone你能帮我吗

3 个答案:

答案 0 :(得分:0)

未声明

do{ ... cursor.moveToNext(); }while(!cursor.isAfterLast()); 变量。它应该是$data

$info

答案 1 :(得分:0)

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

在php文件中添加这两行。这样您就可以共享跨域资源共享

答案 2 :(得分:0)

试试这个,

在插入中查询您没有获得任何数据。

<?php
     $conn = mysqli_connect("localhost", "root", "root", "students");
     $info = json_decode(file_get_contents("php://input"),true);
     if(count($info) > 0) {

               $user = $info['user'];
                $pass = $info['pass'];
                $email=$info['email'];

        $query = "INSERT INTO students(username, password, email) VALUES('".$user."','".$pass."','".$email."')";

        if(mysqli_query($conn, $query)) {
            echo "Insert Data Successfully";
        }
        else {
            echo "Failed";
        }
    }
    ?>