i want to filter data between two dates. i did coding but getting error. Trying to figure out the problem but couldn't . Please help me.
HERE IS THE CODE
<?php
$user='root';
$pass='';
$db='mypro_bms';
$conn = mysqli_connect('localhost',$user,$pass,$db);
if(isset($_POST['search'])){
$txtStartDate=$_POST["txtStartDate"];
$txtEndDate=$_POST["txtEndDate"];
$q=mysqli_query($conn,"SELECT blood_group, SUM(blood_bag) as sum FROM donate where donation_date BETWEEN '$txtStartDate' and '$txtEndDate' order by donation_date");
$count=mysqli_num_rows($q);
}
?>
<body>
<form method="post">
<input type="date" name="txtStartDate">
<input type="date" name="txtEndDate">
<input type="submit" name="search" value="search">
<?php
if ($count=="0")
{
echo "No data"; }
else
{
while ($row=mysqli_fetch_array($q)) {
echo"['".$row['blood_group']."',".$row['sum']."],";
}
}
A variable is being accessed outside of its scope.
The body of the if
statement is ended too early. (This is an example of where indentation can help the human reader.)
$q
and $count
are declared inside the if block; those variables have local scope within that block, and are out of scope outside the block.
if(isset($_POST['search'])) {
// beginning of block
$txtStartDate = $_POST['txtStartDate'];
$txtEndDate = $_POST['txtEndDate'];
$q = ... ;
$count = ... ;
// references to $q and $count are valid inside the block
// block ends at closing curly brace
}
// references to $q and $count are invalid outside the block
Other notes:
mysqli_real_escape_string
)To return rows in a given range of dates for donation_date
Assuming donation_date
column is defined as DATE or DATETIME datatype, the normative pattern to find rows in March 2019 would be something like this:
FROM donation d
WHERE d.donation_date >= '2019-03-01'
AND d.donation_date < '2019-04-01'