2015-11-03 88 views
0

我創建一個高爾夫球賽簽入應用程序,並希望能夠被點擊複選框時,在MySQL數據庫中更新的個人紀錄。所以,當有人登記時,他們按他們的名字點擊複選框,然後我希望它更新該人的數據庫記錄(對於該人的特定身份證,登記欄設置爲1),以便其他人可以看到該人幾乎立即簽入。更新個人MySQL的記錄點擊與AngularJS

我不確定如何在單擊複選框時同時獲取id和checkedin值以僅更新受影響的記錄。

app.js

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

checkinApp.controller('checkinController', function($scope, $http, $location, $anchorScroll) { 

$http.get('lib/api.php').success(function(data) { 
    $scope.checkindata = data; 
}); 


$scope.processForm = function(checkedindata) { 
    $http.post('lib/process.php', { gid : checkedindata.gid, checkedin : checkedindata.checkedin }) 
     .then(function(data) { 
     console.log(data); 
     }) 
    }; 

}); 

HTML /輸出

<table class="table table-striped table-responsive"> 
    <tbody> 
       <tr> 
        <th>Checked In</th> 
        <th>First Name</th> 
        <th>Last Name</th> 
        <th>Company</th> 
        <th>Starting Hole</th> 
       </tr> 
       <tr ng-repeat="item in checkindata"> 
        <td><input type="checkbox" name="checkedin" ng-model='checkedin' ng-change='processForm(checkedindata)' ng-checked="item.checkedin == 1"/></td> 
        <td>{{item.fname}}</td> 
        <td>{{item.lname}}</td> 
        <td>{{item.cname}}</td> 
        <td>{{item.hole}}</td> 
       </tr> 
    </tbody> 
</table> 

process.php

<?php 
require_once('../includes/cred.php'); 


$checkedin = $_POST['checkedin']; 

$sth = $dbh->prepare("UPDATE golfers SET checkedin=$checkedin"); 
$sth->execute(); 

數據庫表 DB Table

輸出 Output

回答

1

只需更換ng-change='processForm(checkedindata)'

通過ng-change='processForm(item)'

,並在你的PHP代碼做這樣的事情:

$sth = $dbh->prepare("UPDATE golfers SET checkedin=$checkedin->checkedin WHERE gid=$checkedin->gid");