1

I am new to angularjs. When I try to insert data into database through angularjs, neither it reports error nor gives any result. I have included 3 files here. I am trying to insert data with this code.

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){

    });
    }

Any help will be highly appreciated,

Thanks

A.K.
  • 3,321
  • 1
  • 15
  • 27
  • What error do you have ? What is the response of the $http request ? – BastienSander Nov 24 '14 at 13:39
  • it does not shows any error thats the problem sir. – SA Sagathiya Nov 24 '14 at 13:43
  • otherwise we can have some solution – SA Sagathiya Nov 24 '14 at 13:44
  • Are you sure the response is a success ? (did you try to log sthg in the succes block and error block ?) – BastienSander Nov 24 '14 at 14:02
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 24 '14 at 14:52
  • Add error reporting to the top of your db.php file right after your opening ` – Jay Blanchard Nov 24 '14 at 14:53

0 Answers0