无法运行查询:Connection.php第673行中的异常:SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2021-09-18 14:35:34

I'm building a laravel application where there is a controller which functionality is we have to book a classroom where if a time has already booked or there in database we can't book a classroom in that time interval. I have used the following controller :

我正在构建一个laravel应用程序,其中有一个控制器,我们必须预订教室,如果时间已经预订,或者在数据库中,我们无法在该时间间隔内预订教室。我使用了以下控制器:

public function postAllocateRoom(Request $request)
    {

            $classRoom = new ClassRoom();  

            $classRoom->department_id=$request->Input(['department_id']);     
            $classRoom->room_id=$request->Input(['room_id']); 
            $classRoom->course_id=$request->Input(['course_id']); 
            $classRoom->day_id=$request->Input(['day_id']); 
            $classRoom->start=$request->Input(['start']); 
            $classRoom->end=$request->Input(['end']);  
            $startTime = Carbon::parse($request->input('start'));
            $endTime = Carbon::parse($request->input('end'));
            $classRoom=DB::select('SELECT allocate_rooms.id 
            FROM allocate_rooms
            WHERE '.$startTime.' BETWEEN allocate_rooms.start AND allocate_rooms.end')->count(); 

            $messages ="Class Room Already Taken";
            if ($classRoom>0) {
             return redirect('allocateRoomPage');
                    }
            else { 
                 $classRoom->save();            
            return redirect('allocateRoomPage');  
            }                   
    }

But I'm getting following Error while saving in database:

但是我在保存数据库时遇到错误:

QueryException in Connection.php line 673: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '17:00:00 BETWEEN allocate_rooms.start AND allocate_rooms.end' at line 3 (SQL: SELECT allocate_rooms.id FROM allocate_rooms WHERE 2016-05-08 17:00:00 BETWEEN allocate_rooms.start AND allocate_rooms.end)

Connection.php第673行中的QueryException:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以便在第17行的“17:00:00 BETWEEN allocate_rooms.start AND allocate_rooms.end”附近使用正确的语法(SQL:SELECT allocate_rooms.id FROM allocate_rooms WHERE 2016-05-08 17:00:00 BETWEEN allocate_rooms.start AND allocate_rooms.end)

How do I solve this?

我该如何解决这个问题?

1 个解决方案

#1


0  

The date needs to be quoted as well:

日期也需要引用:

$classRoom=DB::select('SELECT allocate_rooms.id 
FROM allocate_rooms
WHERE "' . $startTime . '" BETWEEN allocate_rooms.start AND allocate_rooms.end')->count(); 

I would suggest searching for a method to prepare MySQL queries in laravel, instead of writing them by hand.

我建议在laravel中搜索一种准备MySQL查询的方法,而不是手工编写。

Go through this blog post regarding the same: http://fideloper.com/laravel-raw-queries

浏览此博客文章:http://fideloper.com/laravel-raw-queries

#1


0  

The date needs to be quoted as well:

日期也需要引用:

$classRoom=DB::select('SELECT allocate_rooms.id 
FROM allocate_rooms
WHERE "' . $startTime . '" BETWEEN allocate_rooms.start AND allocate_rooms.end')->count(); 

I would suggest searching for a method to prepare MySQL queries in laravel, instead of writing them by hand.

我建议在laravel中搜索一种准备MySQL查询的方法,而不是手工编写。

Go through this blog post regarding the same: http://fideloper.com/laravel-raw-queries

浏览此博客文章:http://fideloper.com/laravel-raw-queries