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')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备