0

So i am new at Angular and never have made a connection to a DB with PHP via Angular. My angular setup is good. The data is coming through but i cannot seem to save it to my DB

This is my code for the html:

<form ng-controller="AppCtrl" name="add_user">
            <div class="modal-header">
                <h3 class="modal-title">Add a user by sending an invite via e-mail</h3>
            </div>
            <div class="modal-body">
                <input type="text" class="form-control" name="user_email" ng-model="user_name" placeholder="Enter a name">
                    <br />
                <input type="text" class="form-control" name="user_name" ng-model="user_email" placeholder="Enter an e-mail adress">
            </div>
            <div class="modal-footer">
                <!-- <button type="" class="btn btn-success" ng-click="add_user()">Invite</button> -->
                <input type="button" class="btn btn-success" name="add_user" value="Invite" ng-click="save_user()">
                <button class="btn btn-warning">Cancel</button>
            </div>
    </form>

This is my app.js code:

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

app.controller('AppCtrl', function($scope, $http){

$scope.save_user = function() {

    $http.post('db.php?action=add_user', 
        {
            'user_name'  : $scope.user_name, 
            'user_email' : $scope.user_email
        }
    )

    .success(function (data, status, headers, config) {
        console.log("The user has been added successfully to the DB");
        console.log(data);
    })

    .error(function(data, status, headers, config) {
        console.log("Failed to add the user to DB ");
    });
}

});

And this is my php code:

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

//echo ('test' . $_GET['action']);
switch($_GET['action'])  {
    case 'add_user' :
        add_user(); 
        break;
}

/**  Function to add user to db **/
function add_user() {

    $data = json_decode(file_get_contents("php://input")); 
    $user_name      = $data->user_name;    
    $user_email     = $data->user_email;

   print_r($data);

    $qry = 'INSERT INTO tblUser(user_name, user_email) VALUES ("' . $user_name  . '","' . $user_email . ')';

    echo ($qry);

    $qry_res = mysql_query($qry);
    if ($qry_res) {
        $arr = array('msg' => "User 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);
    }
}

?>

If any one can point me in the right direction i would really appreciate it

GY22
  • 755
  • 2
  • 17
  • 48
  • Do you initiate a DB connection in your config.php file? – Guinn Jun 24 '15 at 12:48
  • yes sorry i didn't add the code. but the connection is made – GY22 Jun 24 '15 at 12:48
  • 1
    If you can, you should [stop using `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](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 24 '15 at 12:50
  • Add error checking, such as `or die(mysql_error())` to your queries. Or you can find the issues in your current error logs. – Jay Blanchard Jun 24 '15 at 12:50
  • You might also want to check the error logs to see what you have there. – Oladipo Olasemo Jun 24 '15 at 12:58

1 Answers1

3

You're missing a closing double quotes on your email on the insert SQL.

Should be;

$qry = 'INSERT INTO tblUser(user_name, user_email) VALUES ("' . $user_name . '","' . $user_email . '")';

skh
  • 396
  • 6
  • 17