我想过滤两个日期之间的数据。我做了编码但是出错了。试图找出问题,但无法解决。请帮助我。
代码在这里
<?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']."],";
}
}
答案 0 :(得分:0)
正在访问变量范围之外的变量。
if
语句的主体为时过早。 (这是缩进可以帮助读者的示例。)
$q
和$count
在if块中声明;这些变量在该块内具有局部作用域,而在该块外具有作用域。
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
其他说明:
mysqli_real_escape_string
)要返回给定日期范围内donation_date
的行
假设donation_date
列定义为DATE或DATETIME数据类型,则在2019年3月查找行的规范模式将是这样的:
FROM donation d
WHERE d.donation_date >= '2019-03-01'
AND d.donation_date < '2019-04-01'