duandongji2231 2015-11-03 17:55
浏览 45

更新复选框上的单个MySQL记录单击AngularJS

I am creating a golf tournament check-in app and want to be able to update an individual record in the MySQL database when a checkbox is clicked. So, when someone checks in they click the checkbox by their name and I want it to then update the database record of that person (the checked in column set to 1 for the specific id of the person) so that others can see that the person is checked in almost immediately.

I am not sure how to get both the id and the checkedin value to update only the affected record when a checkbox is clicked.

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/Output

<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 DB Table

Output Output

  • 写回答

1条回答 默认 最新

  • dongwoqin7034 2015-11-03 17:58
    关注

    Simply replace ng-change='processForm(checkedindata)'

    by ng-change='processForm(item)'

    and in your PHP code do something like :

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

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划