我正在使用复选框查询数据库,我正在努力解决这个问题,我是MySQL和PHP的新手,很抱歉,如果这很简单!
这是我的代码......
<input type="checkbox" name="season2005" value="2005" <?php if(isset($_POST['season2005'])) echo "checked='checked'"; ?> > 2005-06
<input type="checkbox" name="season2006" value="2006" <?php if(isset($_POST['season2006'])) echo "checked='checked'"; ?> > 2006-07
<input type="checkbox" name="season2007" value="2007" <?php if(isset($_POST['season2007'])) echo "checked='checked'"; ?> > 2007-08
<input type="checkbox" name="season2008" value="2008" <?php if(isset($_POST['season2008'])) echo "checked='checked'"; ?> > 2008-09
<input type="checkbox" name="season2009" value="2009" <?php if(isset($_POST['season2009'])) echo "checked='checked'"; ?> > 2009-10
<input type="checkbox" name="season2010" value="2010" <?php if(isset($_POST['season2010'])) echo "checked='checked'"; ?> > 2010-11
<input type="checkbox" name="season2011" value="2011" <?php if(isset($_POST['season2011'])) echo "checked='checked'"; ?> > 2011-12
<input type="checkbox" name="season2012" value="2012" <?php if(isset($_POST['season2012'])) echo "checked='checked'"; ?> > 2012-13
<input type="checkbox" name="season2013" value="2013" <?php if(isset($_POST['season2013'])) echo "checked='checked'"; ?> > 2013-14
if (@$_POST['season2005'] == ""){ $season2005 = "0000"; } else { $season2005 = "2005"; }
if (@$_POST['season2006'] == ""){ $season2006 = "0000"; } else { $season2006 = "2006"; }
if (@$_POST['season2007'] == ""){ $season2007 = "0000"; } else { $season2007 = "2007"; }
if (@$_POST['season2008'] == ""){ $season2008 = "0000"; } else { $season2008 = "2008"; }
if (@$_POST['season2009'] == ""){ $season2009 = "0000"; } else { $season2009 = "2009"; }
if (@$_POST['season2010'] == ""){ $season2010 = "0000"; } else { $season2010 = "2010"; }
if (@$_POST['season2011'] == ""){ $season2011 = "0000"; } else { $season2011 = "2011"; }
if (@$_POST['season2012'] == ""){ $season2012 = "0000"; } else { $season2012 = "2012"; }
if (@$_POST['season2013'] == ""){ $season2013 = "0000"; } else { $season2013 = "2013"; }
$seasons = array($season2005,$season2006,$season2007,$season2008,$season2009,$season2010,$season2011,$season2012,$season2013);
$seasonpick = implode(",",$seasons);;
$matcharrays = array("AND season in ($seasonpick)");
目前正在向数据库查询所有数据,因此如果没有选择任何数据,那么部分查询是&#34; AND季节(0000,0000,0000,0000)等
我如何只选择那些被选中的数组,如果没有选中,那么数组将是空白的。
希望你理解我的意思!
答案 0 :(得分:0)
这是一个工作表单,其中包含一些复选框,可用于测试和获取您想要的sql。
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode(",", $dateArr);
$sql=".... and season in (".$dateSearch.")";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
echo "<input type=\"checkbox\" name=\"season[]\" value=\"".($i+2005)."\"> ".($i+2005);
}
?>
<input type="submit">
</form>
选择2009年,2010年和2011年时的产出:
....和季节(2009,2010,2011)
好的,它是如何运作的:
如果复选框的名称都以[]
结尾,则最好使用它们。这使它成为一个很好的阵列。
如果设置了后期数据,我们会快速抛出一个唯一的数组(这些类型的查询中的大部分习惯),这样就没有重复的值。
然后简单地将其内爆到一个字符串中并将其弹出到SQL查询中。
编辑:添加了在提交时重新检查复选框的功能。
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode(",", $dateArr);
$sql=".... and season in (".$dateSearch.")";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
$chk="";
if(!empty($_POST['season']))
{
if(in_array($i+2005, $_POST['season']))
{
$chk=" checked=\"checked\" ";
}
}
echo "<input type=\"checkbox\" name=\"season[]\" ".$chk." value=\"".($i+2005)."\"> ".($i+2005);
}
?>
<input type="submit">
</form>
编辑2:只需在正确的位置添加引号:)
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode("', '", $dateArr);
$sql=".... and season in ('".$dateSearch."')";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
$chk="";
if(!empty($_POST['season']))
{
if(in_array(($i+2005)."i", $_POST['season']))
{
$chk=" checked=\"checked\" ";
}
}
echo "<input type=\"checkbox\" name=\"season[]\" ".$chk." value=\"".(($i+2005)."i")."\"> ".($i+2005)."i";
}
?>
<input type="submit">
</form>
编辑3:我觉得这开始真正回答的问题不止一个问题:)
您只需检查文本框以确保它不为空,然后附加到SQL字符串:
$sql="";
if(!empty($_POST['text1']))
{
$sql.=" and ftgf>= ".$_POST['text1']." ";
}
话虽如此,我强烈建议您永远不要让用户输入您将运行的实际SQL的部分内容 - 除非它是一个封闭/安全的环境,这意味着不是一个ope网站。
答案 1 :(得分:0)
插入以下代码
$seasons = array($season2005,$season2006,$season2007,$season2008,$season2009,$season2010,$season2011,$season2012,$season2013);
//start
$seasons2 = array();
foreach ($seasons as $season)
{
if($season!=="0000")
{
array_push($seasons2,$season);
}
}
$seasonpick = implode(",",$seasons2);
//end