php - 当我单击按钮时,我试图根据电子邮件过滤器运行两条语句

标签 php sql laravel

这是我收到的错误

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/

相关文章:

php - View 中的 Laravel undefined variable

php - Laravel 5.0 在源代码中最后生成 app.blade.php

php - 显示通知逻辑

php - 搜索数据库的用户界面

php - 在我的数据库中插入新内容后如何实现通知系统?

php - 在 ob_start 中使用 print_r

mysql - 嵌套 JOIN 在 MySQL 中查找具有多个 WHERE 条件的记录

php - MySQL如何从今天记录的表中选择数据?

sql - 如何截断 MySQL 数据库中的所有表?

mysql - Query1 与 Query2 连接,但 Query2 使用 Query1 的结果,如何仅使用 1 个查询来做到这一点?