Laravel共享主机ajax删除

时间:2018-06-05 22:59:16

标签: ajax laravel shared-hosting

我在freehosting上托管我的网站用于测试目的,一切都运行完美,但Ajax删除。当我点击删除时,删除功能会通过并删除所有内容,但由于某种原因它会返回500错误。在本地它的工作没有问题。

Route::delete('/admin/deleteRound', 'AdminCyclesController@deleteRound')->name('admin.deleteRound');  

$.ajax({
    type: "POST",
    url: urlDeleteRound,
    data: {cycle_id: cycle_id, round: round, _token: token, _method: 'delete'}
}).success(function (response) {.....});

我尝试了一些我能在网上找到但却没有成功的东西。有没有办法解决这个问题,或者至少有办法找出问题所在?

已编辑 - .log

我不知道该怎么做。

  

local.ERROR:SQLSTATE [HY000]:一般错误(SQL:DELETE FROM cycle_team其中cycle_team.cycle_id = 9和cycle_team.round = 1){“userId”:1,“email”:“xxxxxx @ gmail.com“,”exception“:”[object](Illuminate \ Database \ QueryException(代码:HY000):SQLSTATE [HY000]:一般错误(SQL:DELETE FROM cycle_team其中cycle_team.cycle_id = 9和cycle_team.round = 1)at /storage/ssd5/708/6079708/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664,PDOException(代码:HY000):SQLSTATE [HY000]:一般错误at /storage/ssd5/708/6079708/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:332)

编辑2 - 代码执行删除

public function deleteRound(Request $request){

    $round=$request['round'];
    $id=$request['cycle_id'];

    DB::select("DELETE FROM `cycle_team` where cycle_team.cycle_id=$id and cycle_team.round=$round");

    $teams = DB::select("SELECT teams.id, teams.title,sum(ct.points) + sum(ct.is_winner) + sum(ct.is_over) as points, sum(ct.points) + sum(ct.is_winner) + sum(ct.is_over)-Min(ct.points + ct.is_winner + ct.is_over) as minpoints, COUNT(teams.id)-1 as number FROM `teams`INNER JOIN cycle_team as ct on teams.id =ct.team_id INNER JOIN cycles as c on c.id = ct.cycle_id where ct.cycle_id =$id > 0 GROUP BY ct.cycle_id, ct.team_id, teams.title, teams.id order by points desc");

    return response()->json(['teams'=>$teams]);
}

 DB::select("DELETE FROM `cycle_team` where cycle_team.cycle_id=$id and cycle_team.round=$round")

正在制造问题,使用Builder解决问题

DB::table('cycle_team')->where('cycle_id', id)->where('round', $round)->delete();

1 个答案:

答案 0 :(得分:1)

您正在使用DB::select(),默认情况下,它使用只读 PDO实例。由于DELETE是写操作,因此发生了一般错误。

请考虑使用DB::delete()方法而不是DB::select(),因为这是您正在执行的操作类型。

您还可以使用DB::statement(),根据查询的成功返回布尔值,或DB::affectingStatement(),如果您想要查询影响的行数。

或者,正如评论中所建议的那样,使用查询构建器来构建删除查询。

DB::table('cycle_team')
    ->where('cycle_id', $id)
    ->where('round', $round)
    ->delete();