Why doesn't this work? mysql queries

I’m a starting programmer and i need some help with these queries and why they are not working as they should…
Only the else part works correctly atm…
The first part everything works except for the second query summing the prices.

if (!empty($date1_raw) && !empty($date2_raw)) {
        //Query for selecting all costs of the date filter.
        if ($stmt = $conn->prepare("SELECT * FROM costs WHERE costdate BETWEEN ? AND ? AND userid=? AND category=? ORDER BY costdate")) {         
            $stmt->bind_param("ssis", $date1_raw, $date2_raw, $session_id, $category);
            $stmt->execute();
            $result = $stmt->get_result();
        }
        //sum the price column to print to the screen.
        $get_total = "SELECT SUM(price) as total FROM costs WHERE costdate BETWEEN $date1_raw AND $date2_raw AND userid='$session_id' AND category='$category'";
        if ($results = mysqli_query($conn, $get_total)) {
            $show_s = mysqli_fetch_assoc($results);
            $total = $show_s['total'];
        }
        } else {
        // if dates are not entered in filter
        if($stmt = $conn->prepare("SELECT * FROM costs WHERE userid=? AND category=? ORDER BY costdate")) {
            $stmt->bind_param("is", $session_id, $category);
            $stmt->execute();
            $result = $stmt->get_result();
        }
        //sum the price column to print to the screen.
        $get_total = "SELECT SUM(price) as total FROM costs WHERE userid='$session_id' AND category='$category'";
        if ($results = mysqli_query($conn, $get_total)) {
            $show_s = mysqli_fetch_assoc($results);
            $total = $show_s['total'];
        }
    }
    ?>

it’s only a small piece of the script but it basically gets the information that the user is asking for wether it’s being filtered by date or not and then sums up all the prices of the selected rows.

Recently I wrote a little article that explains how to report errors in mysqli

In short, you have to add the following line before mysqli_connect:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

If there is still no error, you have to debug your code to see, whether all variables contain the expected values,

As you’re using dates in queries, have you checked that the date format the user is entering matches the date format that you store in the database table?

If that’s no help, can you expand on “not working as they should” - what happens, and how does it vary from what you expect?

well the first part where it checks if the selected dates are selected, prepare and execute a query to build up a table containing this data works. but the query after that should sum all the prices of the column price inside the selected query but it doesn’t do anything unlike the sum query i use for when the dates are not selected…

So basically it should just show a total which it does when i just load the page but when i filter dates it shows me the correct data but the total is 0 which it shouldn’t be.

If you change the query that’s failing to use bound parameters as you do in the first query, does that make any difference? I’m wondering whether there’s a quotes problem.

The weird part is the sum query for the first part is the same as the sum for the scond part and that one works perfectly the only difference is the BETWEEN with the dates in that query so i’m clueless why that shouldn’t work :frowning:

Everything I read about using BETWEEN with dates talks about casting the input to a date type, and maybe the way you use bound parameters in the first query deals with that for you. Either way, a couple of minutes work to change it and see if it helps, surely?

1 Like

Oh you are right that worked like a charm!
I’m really happy you looked into this and iv’e teached a couple of things again :wink:

Thankyou very much droopsnoot. :grinning:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.