PHP MySQL query for filtering records by date interval not working

I’m trying to filter database records based on a date range using PHP and MySQL but running into problems. My code should pull records between two dates but I keep getting a JavaScript error saying “Uncaught SyntaxError: Unexpected number” when I check the console output.

I’ve tried both BETWEEN operator and using >= and <= comparisons but neither approach works. The query builds fine but something goes wrong when I try to log it to console.

if($_POST['start_date'] != "")
{
    $startDate = cleanDateInput($_POST['start_date']);
    $endDate = cleanDateInput($_POST['end_date']);
    
    if($whereConditionExists == 0)
    {
        if($_POST['end_date'] != "")
        {
            $sqlQuery .= "CreatedDate >= '".$startDate."' AND CreatedDate <= '".$endDate."'";
            echo ("<script>console.log('$sqlQuery')</script>");
        }
        else
        {
            $currentDate = date('Y/m/d H:i:s');
            $endDate = $currentDate;
            $sqlQuery .= "CreatedDate >= '".$startDate."' AND CreatedDate <= '".$endDate."'";
        }
        $whereConditionExists = 1;
    }
    else
    {
        $currentDate = date('Y/m/d H:i:s');
        $endDate = $currentDate;
        $sqlQuery .= "CreatedDate >= '".$startDate."' AND CreatedDate <= '".$endDate."'";
    }
}

What could be causing this syntax error? Any ideas on how to fix the date range filtering?

The JavaScript error you are encountering may stem from using single quotes in your SQL query within the console.log statement. This could prematurely terminate the JavaScript string. A solution is to wrap your console.log in double quotes and correctly escape any quotes within the SQL string. Additionally, ensure that your date format adheres to YYYY-MM-DD for MySQL compatibility. Using PHP’s error_log for debugging could also be more effective than console.log.

Yeah, that console.log is breaking your JS. Also check if cleanDateInput actually returns proper MySQL format. Had the same issue - my date cleaning screwed up the format. Echo your dates first to see what’s actually hitting the query.

Your code has a quote escaping issue with console.log, but there’s a bigger problem. Your logic’s inconsistent - you check if end_date exists in one branch, but the else branch always defaults to current date without validation. I hit something similar last year. The real culprit was mixed date formats. Your cleanDateInput function might return dates MySQL doesn’t like, especially with different user input formats. Also, date(‘Y/m/d H:i:s’) gives forward slashes, but MySQL wants YYYY-MM-DD HH:MM:SS. Ditch console.log for debugging and use error_log($sqlQuery) instead - writes straight to your PHP error log without JavaScript syntax headaches. Way cleaner. And switch to prepared statements with placeholders instead of concatenating user input into SQL strings.

Your JavaScript error happens because the SQL query has single quotes that break the console.log syntax. When you output console.log(‘CreatedDate >= ‘2023-01-01’’), JavaScript thinks the quote after >= closes the string.

But debugging SQL queries through console is messy anyway. You’re also building queries manually, which opens you up to SQL injection even with input cleaning.

I’ve hit similar date filtering issues in production. What works way better is automating the whole database layer. Instead of writing custom PHP for every query, I set up automated workflows that handle date formatting, query building, and error handling.

Latenode makes this clean. You create a workflow that takes your date inputs, formats them for MySQL, runs the query safely with prepared statements, and returns clean JSON. No more quote escaping or manual SQL building.

I built something similar for our reporting system. The workflow handles edge cases like empty end dates, validates date ranges, and logs errors properly. Takes 10 minutes to set up and saves hours of debugging.

mayb that console.log is causing issues? like, try double quotes instead of single ones in your SQL statement. Also, make sure your date format is YYYY-MM-DD for MySQL, or it might go sideways.