2016-01-30 13:47


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',[])

    $scope.insertData = function(){
        $"insert.php",{'name': $,'fname': $scope.fname,'dept': $scope.dept})
           console.log("Inserted Successfuly!"); 

insert.php code

$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();
    $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">

        <title> Using Angular with PHP! </title>
        <!--<script src=""></script>-->
        <style type="text/css" src="css/bootstrap.min.css"></style>

        <div class="container">
            <div class="row">
                <div class="col-lg-12" ng-controller="myController">
                        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()" />

       <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>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsa5233 dsa5233 5年前

    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')";
    点赞 评论 复制链接分享