字符过滤问题

#1 homexigua

                $this->menuname = '教室使用情况查询';
                $db = spClass('lib_course');
                $sql = "select * from c_course where 1=1";

                $course_flag=(int)$this->spArgs('course_flag',1); //默认有课
                $this->course_flag = $course_flag;
                //检索
                //日期检索
                $course_date = $this->spArgs('course_date',false);
                $this->course_date = $course_date;
                $week = date('w',strtotime($course_date));//通过日期获得星期
                $weekly = (int)(((strtotime($course_date)-strtotime('2010-8-23'))/86400 +1)/7+1); //获取周数
                if($course_date!=false){
                        $condition = " and course_week=$week and substr(course_weekly,$weekly,1)=$course_flag";
                        $sql.=$condition;
                }
                //星期
                $course_week = (int)$this->spArgs('course_week',0);
                $this->course_week=$course_week;
                if($course_week!=0 and $course_date==false){
                        $condition = " and course_week=$course_week";
                        $sql .= $condition;
                }
                //课程名
                $course_name = $db->escape($this->spArgs('course_name',false));
                $course_name = trim($course_name,"'");
                $this->course_name=$course_name;
                if($course_name!=false){
                        $condition = " and course_name like '%$course_name%'";
                        $sql .= $condition;
                }
                //教师
                $course_teacher = $db->escape($this->spArgs('course_teacher',false));
                $course_teacher = trim($course_teacher,"'");
                $this->course_teacher=$course_teacher;
                if($course_teacher!=false){
                        $condition = " and course_teacher like '%$course_teacher%'";
                        $sql .= $condition;
                }
                //教室
                $course_room = $db->escape($this->spArgs('course_room',false));
                $course_room = trim($course_room,"'");
                $this->course_room=$course_room;
                if($course_room!=false){
                        $condition = " and course_room like '%$course_room%'";
                        $sql .= $condition;
                }
                //节次为数字
                $course_jieci = (int)$this->spArgs('course_jieci',0);
                $this->course_jieci=$course_jieci;
                if($course_jieci!=0 and $course_time==0){
                        $condition = " and course_jieci=$course_jieci";
                        $sql .= $condition;
                }
                //周次
                $course_weekly = (int)$this->spArgs('course_weekly',0);
                $this->course_weekly=$course_weekly;
                if($course_weekly!=0 and $course_date==false){
                        $condition = " and substr(course_weekly,$course_weekly,1)=$course_flag";
                        $sql .= $condition;
                }
                //班级
                $course_class = $db->escape($this->spArgs('course_class',false));
                $course_class = trim($course_class,"'");
                $this->course_class=$course_class;
                if($course_class!=false){
                        $condition = " and course_class like '%$course_class%'";
                        $sql .= $condition;
                }

                $sql.=" order by course_room,course_jieci";

                $this->list = $db->spPager($this->spArgs('p',1),14)->findSql($sql);
                $this->pager = $db->spPager()->getPager();
                echo $db->dumpSql();
以上代码中只要涉及数字的文本框使用escape就会什么也检索不出来,也无法翻页。escape是否只对字符串有效?可否弄个我们菜鸟通用的过滤,省的还得每次鉴别需要不需要使用escape

2010-09-20 09:08:25

#2 jake

当涉及到数字的时候,不能使用trim。escape是很标准的过滤方式,对字符串,数字等都可以起效。楼主的问题在于把数字类型的变量也trim了,所以无法得出结果。

“可否弄个我们菜鸟通用的过滤,省的还得每次鉴别需要不需要使用escape”,这句话就有些:dizzy:了,当find和findAll条件是数组的时候,escape是自动执行的。已经是极大方便了普通的开发者和日常的开发工作。

如果是SQL语句的话,那么escape也不能知道,哪个地方是需要过滤的,哪个是不需要,这个处理,目前就算是PHP自带函数库或者是MYSQL本身也做不到,(如果能做到,那么我们根本不需要自己去过滤输入的变量,对吧)

2010-09-20 09:35:05

#3 homexigua

还得麻烦一下,那象这种数字型的如何写才正确?我没发现我用了trim呀
                 //节次为数字
                 $course_jieci = (int)$this->spArgs('course_jieci',0);
                 $this->course_jieci=$course_jieci;
                 if($course_jieci!=0 and $course_time==0){
                         $condition = " and course_jieci=$course_jieci";
                         $sql .= $condition;
                 }

2010-09-20 15:32:32

#4 jake

还得麻烦一下,那象这种数字型的如何写才正确?我没发现我用了trim呀
homexigua 发表于 2010-9-20 15:32
你已经直接用(int)了,就不需要escape过滤了,也就是这里的代码是正确的。

2010-09-20 15:48:24