php - 存储过程出现问题,删除函数运行,但未对数据库进行删除。 PHP、MySQL、AngularJS。

标签 php mysql angularjs html

我的数据库中有两个表,“tbl_player”和“tbl_teams”。

我正在尝试实现一个存储过程,如果我删除一个团队,那么玩家也应该被删除。问题是一切都运行没有错误,我可以在控制台中看到数据已删除,但数据库没有任何更改。

(html底部的按钮正在调用php函数)

HTML

<div class="container wrapper">
 <h1 class="text-center">Players in database</h1>
 <br/>
 <div class="">
  <nav class="navbar navbar-dark bg-dark">

   <form >
    <a><button class="btn btn-primary" ng-click="add_player()">Add 
      Player <span></span>
    </button></a>
   </form>
   <div>
    <span class="input-group-btn">
     <input type="text" class="form-control" placeholder="Search 
         Player">
    </span>
   </div>
  </nav>

  <div class="table-responsive">
   <table class="table table-hover">
    <tr>
     <th>Player ID</th>
     <th>Name</th>
     <th>Email</th>
     <th>Team</th>
    </tr>

    <tr ng-repeat="user in users track by $index">
     <td>{{user.PlayerID}}</td>
     <td>{{user.PlayerName}}</td>
     <td>{{user.PlayerEmail}}</td>
     <td>{{user.TeamName}}</td>
     <td>
       <button class="btn btn-warning" ng- 
          click="update_player(user.PlayerID, user.PlayerName, 
          user.PlayerEmail, user.TeamName)"><span ></span>Edit
       </button>
     </td>

     <td>
      <button class="btn btn-danger" ng-click="delete(user.PlayerID)" 
        title="PlayerDelete"><span></span>Delete
      </button>
     </td>
    </tr>
   </table>
  </div>
 </div>

 <div class="">
  <div class="table-responsive" >
   <table class="table table-hover">
    <tr>
     <th>Team ID</th>
     <th>Team Name</th>
    </tr>
    <tr ng-repeat="teams in teams track by $index">
     <td>{{teams.TeamID}}</td>
     <td>{{teams.TeamName}}</td>  
     <td>
      <button class="btn btn-danger" ng- 
       click="deleteTeam(teams.TeamID)" title="PlayerDelete"><span> 
       </span>Delete
      </button>
     </td>
    </tr>
   </table>
  </div>
 </div>
</div>

teamDelete.php

<?php
include 'DbConfig.php';

$data = json_decode(file_get_contents("php://input"));

$user = $data->id;

$sql = "CALL deleteTeam ('$user')";

if (mysqli_query($conn, $sql)) {
  echo "Team was deleted";
}
else {
 echo "Err Could not delete Team";
}
mysqli_close($conn);
?>

玩家 Controller

  $scope.deleteTeam = function(id) {
   console.log(id);
   $http.post("teamDelete.php", {'id':id});
   console.log("Deleted");
   $location.path('/');
   console.log("page loaded again");
  };

MySql

CREATE DEFINER=`root`@`localhost` PROCEDURE `deleteTeam`(IN id 
INT(255))
BEGIN
DECLARE EXIT HANDLER FOR SQLWARNING

BEGIN 
ROLLBACK;
END;

START TRANSACTION;

DELETE FROM tbl_player WHERE PlayerTeam = id;
DELETE FROM tbl_teams WHERE TeamID = id; 

END

最佳答案

似乎缺少 COMMIT 语句。

如果设计是在存储过程中处理事务,则该过程似乎应该完成事务,并在过程中执行COMMIT

按照目前的编写,当过程调用返回时,我们会回显“团队已删除”,但数据库中的工作实际上尚未完成。

调用者只知道过程返回。它不知道是否遇到警告,从而导致发出 ROLLBACK 语句。

关于php - 存储过程出现问题,删除函数运行,但未对数据库进行删除。 PHP、MySQL、AngularJS。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101809/

相关文章:

php - 尝试使用 user_id Laravel 从用户模型中获取用户名

mysql - 如何区分两个字母相似的ID

angularjs - "search"上的 Angular 数据表搜索过滤器按

php - 对网站进行地理定位并仍然使 seo 正常工作

php - 如何从config.php获取数据库名称

mysql - MySQL中用IF条件触发

javascript - AngularJS 1事件-回调方法中的 "this"变量是窗口

angularjs - Owl-Carousel 不适用于 AngularJs ng-repeat

php - 文件上传 : Size validator or setMaxFileSize()

mysql - 使用 Diesel、r2d2 和 r2d2-diesel 查询数据库时出错