dou31797719 2011-05-02 18:20
浏览 29
已采纳

php按用户过滤

ive got list of reports. for default, the report list will be showing all reports without filtering. when drop down filter click, it will filter the result by name. anyidea how to fix it?

function getReportSingleMonth($month, $year, $id_user=NULL) {
   $month = $db->real_escape_string($month);
   $year = $db->real_escape_string($year);
   $db->query("SELECT * FROM user WHERE MONTH(date)='$month' AND YEAR(date)='$year' AND id_user='$id_user'");
}

the html part:

<form method="post" name="report_filter" action="<?= $_SERVER['PHP_SELF'];?>?report&month=<?= $_GET['month'];?>&year=<?= $_GET['year'];?>">
<div align="right"><select name="user_name" onchange="report_filter.submit();"><option value="--">Filter by:</option><option value="1">Andi</option>M<option value="2">Jenny</option><select></div>    
<? if(isset($_POST['user_name'])):
    $admin->getReportSingleMonth($_GET['month'], $_GET['year'], $_POST['user_name'])     
else :
    $admin->getReportSingleMonth($_GET['month'], $_GET['year']);
endif;
?>
</form>
  • 写回答

3条回答 默认 最新

  • douru5373 2011-05-02 18:35
    关注

    In older versions of MySQL you can't put quotes around integer values. Try switching this line:

    $db->query("SELECT * FROM user WHERE MONTH(date)='$month' AND YEAR(date)='$year'" AND id_user=$id_user");
    

    Looking again, I noticed that you're not always going to pass a user_id. With that in mind the function should be changed:

    function getReportSingleMonth($month, $year, $id_user=NULL) {
        $month = $db->real_escape_string($month);
        $year = $db->real_escape_string($year);
        $query = "SELECT * FROM user WHERE MONTH(date)='$month' AND YEAR(date)='$year'";
        if ( is_int( $id_user ) ) {
            $query .= ' and id_user=' . $id_user;
        }
        $db->query( $query );
    }
    

    Now the id_user part of the query is only added if it was passed to the function.

    I also recommend using sprintf

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程