dptgpyl61857413
2016-08-23 05:49
浏览 33
已采纳

尝试使用Angularjs更新数据库中的数据

I am trying to update data in the database using angular in Laravel 4 but the data is'nt get inserted, and a blank value is getting stored in to the database. Please find the bug and try to insert the passed value insteed of blank value.

JS File

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $scope.updateFunction = function(updateId) {
    $http.get("http://localhost/crud/public/registration_update_page/" + updateId).then(function successCallback(response) {
      console.log('successCallback');
      console.log(response);
      alert("Row with id " + updateId + ", updated..!");
    }, function errorCallback(response) {
      console.log('errorCallback');
      console.log(response);
    });
  };
});

.PHP File

<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

</head>
<body ng-app="myApp" ng-controller="myCtrl">

<h2>Registration</h2><br/>
<table border=1>
<tr><td>Name:</td><td><input type="text" ng-model="nam" name="nam"><br/></td></tr>
<tr><td>Email:</td><td><input type="email" ng-model="email" name="email"><br/></td></tr>
<tr><td>Password:</td><td><input type="password" ng-model="password" name="password"><br/></td></tr>
<tr><td>City:</td><td><input type="text" ng-model="city" name="city"><br/></td></tr>
<tr><td><button ng-click="insertFunc()" type="submit">Submit</button></td></tr>
</table>

</div>
</form>

<table style="width:100%" border=1>
  <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Email</th>
    <th>Password</th>
    <th>City</th>
    <th>Update</th>
    <th>Delete</th>

  </tr>

  <tr ng-init='show = true' ng-repeat="x in query" ng-hide='!show'></div>
    <!-- ng-hide will work when it is true in condition -->
    <td>{{x.id}}</td>
    <td><input type="text" value="{{x.name}}" name="edited_name"></td>
    <td><input type="text" value="{{x.email}}" name="edited_email"></td>
    <td><input type="text" value="{{x.password}}" name="edited_password"></td>
    <td><input type="text" value="{{x.city}}" name="edited_city"></td><br/>
    <td><input type="submit" ng-click="updateFunction(x.id);" value="Update"></td>
    <td><button ng-click="deleteFunction(x.id);show = false">Delete</button></td>

    <!-- !show==false means !false i.e., true -->
  </tr><div>
</body>
</html>

Controller

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Session;
use Redirect;

class NewController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
public function registration_update_function(Request $request)
    {
      $updateId = $request->id;
      $edited_name = $request->edited_name;
      $edited_city = $request->edited_city;
      $users1 = DB::table('angular_registration')->where('id', '=', $updateId)->update(['city' => $edited_city]);
    }
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanpiangeng8958 2016-08-23 10:55
    已采纳

    I solved this problem myself This works for ANGULAR.JS with Laravel 4 to run insert,update,delete and select queries of the MySql DataBase.

    VIEW

    <!DOCTYPE html>
    <html>
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
    
    <h2>Registration</h2><br/>
    <table border=1>
    <tr><td>Name:</td><td><input type="text" ng-model="nam" name="nam"><br/></td></tr>
    <tr><td>Email:</td><td><input type="email" ng-model="email" name="email"><br/></td></tr>
    <tr><td>Password:</td><td><input type="password" ng-model="password" name="password"><br/></td></tr>
    <tr><td>City:</td><td><input type="text" ng-model="city" name="city"><br/></td></tr>
    <tr><td><button ng-click="insertFunc()" type="submit">Submit</button></td></tr>
    </table>
    
    </div>
    </form>
    
    <table style="width:100%" border=1>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Password</th>
        <th>City</th>
        <th>Update</th>
        <th>Delete</th>
    
      </tr>
    
      <tr ng-init='show = true' ng-repeat="x in query" ng-hide='!show'></div>
        <!-- ng-hide will work when it is true in condition -->
        <td>{{x.id}}</td>
        <td>{{x.name}}<input type="text" ng-model="edited_name"></td>
        <td>{{x.email}}<input type="text" ng-model="edited_email"></td>
        <td>{{x.password}}<input type="text" ng-model="edited_password"></td>
        <td>{{x.city}}<input type="text" ng-model="edited_city"></td><br/>
        <td><input type="submit" ng-click="updateFunction(x.id,edited_name,edited_email,edited_password,edited_city);" value="Update"></td>
        <td><button ng-click="deleteFunction(x.id);show = false">Delete</button></td>
    
        <!-- !show==false means !false i.e., true -->
      </tr><div>
    
    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope,$http) {
    
       $scope.deleteFunction = function(deleteId)
              {
                $http.get("http://localhost/crud/public/registration_delete_page/"+deleteId)
                .then(function successCallback(response)
                {
                  console.log('successCallback');
                  console.log(response);
                  alert("Row with id "+deleteId+", deleted..!");
                },
                function errorCallback(response)
                {
                  console.log('errorCallback');
                  console.log(response);
                });
              };
        $scope.updateFunction = function(updateId,edited_name,edited_email,edited_password,edited_city)
              {
                $http.get("http://localhost/crud/public/registration_update_page/"+updateId+"/"+edited_name+"/"+edited_email+"/"+edited_password+"/"+edited_city)
                .then(function successCallback(response)
                {
                  //$scope.edited_name = edited_name;
                  console.log('successCallback');
                  console.log(response);
                  alert("Row with id "+updateId+", updated..!");
                },
                function errorCallback(response)
                {
                  console.log('errorCallback');
                  console.log(response);
                });
    
              };
    
        $scope.insertFunc = function() {
            $http({
              method  : 'POST',
              url     : 'http://localhost/crud/public/registration_data_page',
              data    : {nam:$scope.nam,email:$scope.email,password:$scope.password,city:$scope.city}
             }).then(function successCallback(response) {
              console.log('successCallback');
              console.log(response);
    
      }, function errorCallback(response) {
                console.log('errorCallback');
              console.log(response);
      });
    
    }
    
    $http({method:'GET', url:'http://localhost/crud/public/registration_json_page'}).success(function(response){
    $scope.query = response;
    });
    });
    </script>
    
    </body>
    </html>
    

    Controller

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Http\Requests;
    use Illuminate\Http\Request;
    use DB;
    use Session;
    use Redirect;
    
    class NewController extends Controller
    {
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
        }
    public function registration_data_function(Request $request)
        {
          echo $nam_value = $request->nam;
          echo $email_value = $request->email;
          echo $password_value = $request->password;
          echo $city_value = $request->city;
    
          $reg = DB::table('angular_registration')->insert(['name' => $nam_value, 'email' => $email_value, 'password'=>$password_value, 'city'=>$city_value]);
        }
    
        public function registration_json_function(Request $request)
        {
          $query = DB::select('select * from angular_registration');
          return response($query);
        }
    
        public function registration_function(Request $request)
        {
          $query = DB::select('select * from angular_registration');
          return view('registration');
        }
    
        public function registration_delete_function(Request $request)
        {
          $deleteId = $request->id;
          return $users = DB::table('angular_registration')->where('id', '=', $deleteId)->delete(); 
        }
    
        public function registration_update_function(Request $request)
        {
          echo $updateId = $request->id;
          echo $edited_name = $request->edited_name;
          echo $edited_email = $request->edited_email;
          echo $edited_password = $request->edited_password;
          echo $edited_city = $request->edited_city;
    
          $users1 = DB::table('angular_registration')->where('id', '=', $updateId)->update(['name' => $edited_name,'email' => $edited_email,'password' => $edited_password,'city' => $edited_city]);
        }
    }
    

    ROUTE

    <?php
    
        /*
        |--------------------------------------------------------------------------
        | Application Routes
        |--------------------------------------------------------------------------
        |
        | Here is where you can register all of the routes for an application.
        | It's a breeze. Simply tell Laravel the URIs it should respond to
        | and give it the controller to call when that URI is requested.
        |
        */
    
        Route::get('/', function () {
            return view('welcome');
        });
    
        Route::auth();
    
    Route::get('/registration', 'NewController@registration_function');
    
    Route::post('/registration_data_page', 'NewController@registration_data_function');
    
    Route::get('/registration_json_page', 'NewController@registration_json_function');
    
    Route::get('registration_delete_page/{id}', 'NewController@registration_delete_function');
    
    Route::get('registration_update_page/{id}/{edited_name}/{edited_email}/{edited_password}/{edited_city}', 'NewController@registration_update_function');
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douju2474 2016-08-23 07:44

    Try with post method and following code and pass full x object / scope and x.id in this function updateFunction(x, x.id);

    Angular Code

    var req = {
     method: 'POST',
     url: 'http://localhost/crud/public/registration_update_page/',
     data: { x: x, id = x.id }
    }
    
    $http(req).then(function(){
       //Success
    }, function(){
    
    });
    

    On server side PHP

    print_r($_POST);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题