使用更新查询

时间:2016-04-12 14:37:31

标签: php mysql angularjs

我正在使用Angular和SLIM开发一个REST API,其中包含有关团队和玩家的信息。我的POST,GET和DELETE的工作正常,但是当我尝试发布到玩家表时我收到了错误。错误消息是:

参数号无效:绑定变量数不匹配

我查看了所有绑定变量,但无法查看问题所在。我已经在下方展示了一些相关代码,希望有人可以帮助我。

HTML

<div ng-show="editorEnabled">
                                <p>Name
                                    <input ng-model="player.player_name">
                                </p>
                                <p>DOB
                                    <input ng-model="player.player_dob">
                                </p>
                                <p>Nationality
                                    <input ng-model="player.player_nationality">
                                </p>
                                <p>Position
                                    <input ng-model="player.player_position">
                                </p>
                                <p>Club
                                    <input ng-model="player.club_name">
                                </p>
                                <a href="#" ng-click="editorEnabled=!editorEnabled; updatePlayer(player)">Save <span class="fa fa-save"></span></a>
                            </div>

JS / Angular Controller

$scope.updatePlayer = function (player) {

    $scope.player = player;
    var id = $scope.player.id;
    console.log($scope.player);

    $http({
        method: 'PUT',
        url: 'players.php/players/' + id,
        data: $scope.player,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });

}; 

此时,控制台显示信息已正确发送并按预期发送。

PHP

     function updatePlayer($id){
  $request = Slim::getInstance()->request();
    $body = $request->getBody();
    $player= json_decode($body);
    $sql = "Update players SET player_name=:player_name, player_position=:player_position, player_nationality=:player_nationality, player_dob=:player_dob WHERE id=:id";
try{
    $db = getConnection();
     $stmt =$db->prepare($sql);
    $stmt->bindParam("player_name", $player->player_name);
    $stmt->bindParam("player_position", $player->player_position);
    $stmt->bindParam("player_nationality", $player->player_nationality);
    $stmt->bindParam("player_dob", $player->player_dob);
    $stmt->bindParam("club_name", $player->club_name);
    $stmt->bindParam("id", $id);
    $stmt->execute();
    $db = null;
    responseJson(json_encode($player),200);    
}catch(PDOException $e){
    responseJson('{"error":{"text":'.$e->getMessage().'}}', 500);

}   
}

1 个答案:

答案 0 :(得分:2)

正如错误消息所示,绑定到该语句的变量数与更新查询中实际使用的变量数不匹配:

club_name查询中从不使用UPDATE参数。只需删除

即可

$stmt->bindParam("club_name", $player->club_name);

或将club_name添加到查询中,您应该没问题。