【问题标题】:Laravel Shared hosting ajax deleteLaravel 共享主机 ajax 删除
【发布时间】:2018-06-05 22:59:15
【问题描述】:

出于测试目的,我在 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 where 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_team where cycle_team.cycle_id=9 and cycle_team.round= 1)在/storage/ssd5/708/6079708/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664,PDOException(代码: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();

【问题讨论】:

  • 检查 laravel.log 看看是什么导致了错误
  • 请检查编辑,我迷路了。
  • 你能贴出执行删除的代码吗?
  • 已发布,有什么见解吗?复制到服务器时是否有可能某些文件损坏?
  • 您是否尝试过使用QueryBuilder 执行删除? - DB::table('cycle_team')->where('cycle_id', $id)->where('round', $round)->delete();

标签: ajax laravel shared-hosting


【解决方案1】:

您使用的是DB::select(),它在后台默认使用只读 PDO 实例。由于DELETE是写操作,所以出现一般错误。

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

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

或者,按照 cmets 中的建议,使用查询构建器构建删除查询。

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

【讨论】:

  • @StanleyHuxley - 这是一个更全面的解释。我建议接受这个答案。
  • 感谢您的解释。这很有帮助。
猜你喜欢
  • 1970-01-01
  • 2017-06-28
  • 2018-06-21
  • 2023-03-31
  • 1970-01-01
  • 2018-01-13
  • 2018-10-21
  • 2015-04-12
  • 1970-01-01
相关资源
最近更新 更多