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.