这是我收到的错误
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 '[email protected]'.' GROUP BY email, date' at line 2 (SQL: SELECT email, date, min(time) AS checkedin, max(time) AS checkedout,( (TIME_TO_SEC(TIMEDIFF(max(time), min(time))) / 60) / 60) difference↵ FROM profile WHERE '. 1=1 and email like '[email protected]'.' GROUP BY email, date)"
我正在尝试根据提供的电子邮件和单击按钮来过滤数据。第一个查询运行良好,但当我尝试在第二个查询中使用相同的 where 条件时出现错误。
$post = $request->all();
$email = $request->input('email');
$cond = ' 1=1 ';
if(!empty($post['email'])){
$cond .= " and email like '".$post['email']."'";
}
$qry = 'SELECT User_id, email, status, date, time FROM profile WHERE '.$cond.' ';
$data = DB::select($qry);
$sql=" SELECT email, date, min(time) AS checkedin, max(time) AS checkedout,( (TIME_TO_SEC(TIMEDIFF(max(time), min(time))) / 60) / 60) difference
FROM profile WHERE '.$cond.' GROUP BY email, date";
$previousdata = DB::select($sql);
最佳答案
我已将上面的代码编辑为正确的代码。该错误是由于字符串连接造成的
$sql="SELECT email, date, min(time) AS checkedin, max(time) AS checkedout,( (TIME_TO_SEC(TIMEDIFF(max(time), min(time))) / 60) / 60) difference
FROM profile WHERE".$cond. "GROUP BY email, date";
关于php - 当我单击按钮时,我试图根据电子邮件过滤器运行两条语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56472768/