首页 > 解决方案 > Laravel 共享主机 ajax 删除

问题描述

我在 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_teamwhere cycle_team.cycle_id=9 and cycle_team.round=1) {"userId":1,"email":"xxxxxx@gmail.com","exception ":"[object] (Illuminate\Database\QueryException(code: HY000): SQLSTATE[HY000]: 一般错误 (SQL: DELETE FROM cycle_teamwhere cycle_team.cycle_id=9 and cycle_team.round=1) at /storage/ssd5/708 /6079708/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: HY000): SQLSTATE[HY000]: 一般错误在 /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();

标签: ajaxlaravelshared-hosting

解决方案


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

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

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

或者,按照评论中的建议,使用查询生成器构建删除查询。

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

推荐阅读