当我尝试通过angularjs将数据插入数据库时​​,它既不会给出错误也不会给出任何结果。怎么解决?

时间:2014-11-24 13:36:12

标签: php angularjs

我是angularjs的新手。当我尝试通过angularjs将数据插入数据库时​​,它既不会报告错误也不会给出任何结果。 我在这里包含了3个文件。我正在尝试使用此代码插入数据。

的index.html

<form name="add_product" method="post">
                        <div class="lable_left">
                            <p>First Name :</p>
                            <p>Last Name :</p>
                            <p>Address :</p>
                            <p>Age :</p>
                            <p>Department :</p>
                        </div>
                        <div class="input_left">

                            <p><input type="text" name="fname" ng-model="fname"></p>
                            <p><input type="text" name="lname" ng-model="lname"></p>
                            <p><input type="text" name="address" ng-model="address"></p>
                            <p><input type="text" name="age" ng-model="age"></p>
                            <p><input type="text" name="dept" ng-model="dept"></p>
                            <input type="hidden" name="eid" ng-model="eid" />
                            <input type="button" name="submit_product" ng-show='add_prod' value="Submit" ng-click="product_submit()" class="submitbutton">
                            <input type="button" name="update_product" ng-show='update_prod' value="Update" ng-click="update_product()" class="submitbutton">
                        </div>
                        </form>

db.php中

    <?php
    include('config.php');

    switch($_GET['action']) {
    case 'add_product' :
    add_product();
    break;
    }

    function add_product() {
    $data = json_decode(file_get_contents("php://input"));

    $fname = $data->fname; 
    $lname = $data->lname;
    $address = $data->address;
    $age = $data->age;
    $dept = $data->dept;

    print_r($data);

    $qry = 'INSERT INTO employee (efname,elname,eaddress,eage,edept) values ("' . $fname . '","' . $lname . '",' .$address . ','.$age.','.$dept.')';

    $qry_res = mysql_query($qry);
    if ($qry_res) {
    $arr = array('msg' => "Product Added Successfully!!!", 'error' => '');
    $jsn = json_encode($arr);
    // print_r($jsn);
    } 
    else {
    $arr = array('msg' => "", 'error' => 'Error In inserting record');
    $jsn = json_encode($arr);
    // print_r($jsn);
    }
    }
    ?>

controller.js

    var listApp = angular.module('listpp', []);
    listApp.controller('PhoneListCtrl', function ($scope,$http) {

    /** function to get detail of product added in mysql referencing php **/

    $scope.get_product = function() {
    $http.get("db.php?action=get_product").success(function(data)
    {
    //$scope.product_detail = data; 
    $scope.pagedItems = data;

    });
    }

    /** function to add details for products in mysql referecing php **/

    $scope.product_submit = function() {
    $http.post("db.php?action=add_product", 
    {
    'efname'   : $scope.fname, 
    'elname'   : $scope.lname, 
    'eaddress' : $scope.address,
    'eage'     : $scope.age,
    'edept'    : $scope.dept
    }
    )
    .success(function (data, status, headers, config) {
    $scope.get_product();
    })

    .error(function(data, status, headers, config){

    });
    }

任何帮助都将受到高度赞赏,

谢谢

0 个答案:

没有答案