dpowhyh70416 2019-04-16 20:25
浏览 163

如何在两个日期之间过滤数据

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']."],";
              }
            }
  • 写回答

1条回答 默认 最新

  • dotwc62080 2019-04-16 20:39
    关注

    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:

    • code pattern appears to be vulnerable to SQL Injection
    • variables included in SQL text must be properly escaped (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'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用