douding_1073 2016-01-30 13:47
浏览 60
已采纳

为什么我要尝试在AngularJs中获取非对象错误的属性?

I am new to Angular JS , i want to insert data from form to MySQL database table ( in my case table name is employe ) But when i hit submit it is not inserting . I compiled insert.php file in browser which gives me this error "Trying to get property of non-object" . I am sending data in JSON format and then i have decoded in the index.php file but when i tried to access the decoded data via $data object it gives me the mentioned error .

myApp.js code

var app = angular.module('myApp',[])

.controller("myController",function($scope,$http){
    $scope.insertData = function(){
        $http.post("insert.php",{'name': $scope.name,'fname': $scope.fname,'dept': $scope.dept})
        .success(function(data,status,headers,config){
           console.log("Inserted Successfuly!"); 
        });
    }
});

insert.php code

<?php
$data = json_decode(file_get_contents("php://input"));
$name = $data->name;
$fname = $data->fname;
$dept = $data->dept;

$host = 'localhost';
$dbname = 'company';
$dbusername = 'root';
$dbpass = '';

$connection = mysqli_connect($host,$dbusername,$dbpass,$dbname);

if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
    $query = "INSERT INTO employe ('id','ename','fname','dept') VALUES('','$name','$fname','$dept')";
    $query_run = mysqli_query($connection,$query);
}
?>

index.html code

<html ng-app="myApp">

    <head>
        <title> Using Angular with PHP! </title>
        <!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular.min.js"></script>-->
        <style type="text/css" src="css/bootstrap.min.css"></style>
    </head>

    <body>   
        <div class="container">
            <div class="row">
                <div class="col-lg-12" ng-controller="myController">
                    <form>
                        Your Name:   <input type="text" ng-model="name" /><br><br>
                        Father Name: <input type="text" ng-model="fname" /><br><br>
                        Department : <input type="text" ng-model="dept" /><br><br>
                        <input class="btn btn-success" type="button" value="Submit" ng-click="insertData()" />
                    </form>
                </div>
            </div>
        </div> 


       <script type="text/javascript" src="js/angular/angular.min.js"></script>
       <script type="text/javascript" src="js/bootstrap/bootstrap.min.js"></script>
       <script type="text/javascript" src="js/app.js"></script>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • dsa5233 2016-02-15 09:23
    关注

    You will laugh but just by adding these `` around your table name would do your work fine

      $query = "INSERT INTO `employe`(`id`, `ename`, `fname`, `dept`) VALUES('','$name','$fname','$dept')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?