sql中对于传过来的空值判断,该怎么处理

WBOY
发布: 2016-06-13 13:13:39
原创
815 人浏览过

sql中对于传过来的空值判断
我有四个这样的输入值
action到处理的php页面
有没有什么方法能够合理的让php页面的sql语句判断当那个字段为空时,就不以那个为搜索的条件了
除了穷举,还有什么方法?
$sql="select * from table where ..."


实例:
html


姓名:
年龄:
性别:
身高:


chuli.php
//连接数据库部分省略
$sql="select * from student where name='$name' and age='$age' and sex='$sex' and height='$height';// 这个是所有的  
$result = mysql_query($sql,$con);

//除了穷举以外,有没有什么方法,判断那几个Input数据为空的时候的$sql进行判断的方法?
//如果是4个input的情况下的时候是 4+6+4+1+1=16种情况,应该对吧? c41+c42+c43+c44+1=4+6+4+1+1=16?

------解决方案--------------------
PHP code
foreach(array_diff($_POST, array('')) as $k=>$v)
  $r[] = "$k='$v'";

$sql = "select * from student where ". join(' and ', $r);
<br><font color="#e78608">------解决方案--------------------</font><br>应该是,上面少给一个''<br><br>
登录后复制
PHP code
$sql="select * from student where 1=1";
if(!empty($_POST['name'])) $sql=" and name='$_POST[name]'";
if(!empty($_POST['age'])) $sql=" and age='$_POST[age]'";
if(!empty($_POST['sex'])) $sql=" and sex='$_POST[sex]'";
if(!empty($_POST['height'])) $sql=" and height='$_POST[height]'"; <div class="clear">
                 
              
              
        
            </div>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!