如题,上一个页面有4个check box,b,c,d,e 勾选则取值,不勾选值为空,
SELECT 语句如下:Select a,b,c,d,e,f from table A,table B where ....
若b,c d,e可能为空的话,逗号在SQL语句中也需要以变量表示,否则逗号多余就会报错,
请教一下写法,怎样才能把上述情况实现呢?
另: 假设$testa=$_POST['a']; 那么在SQL 语句中$sql = "select ".$testa." ..."; 是这样么?
若可以请提供一下具体的代码写法,十分感谢!!!
SELECT 语句如下:Select a,b,c,d,e,f from table A,table B where ....
若b,c d,e可能为空的话,逗号在SQL语句中也需要以变量表示,否则逗号多余就会报错,
请教一下写法,怎样才能把上述情况实现呢?
另: 假设$testa=$_POST['a']; 那么在SQL 语句中$sql = "select ".$testa." ..."; 是这样么?
若可以请提供一下具体的代码写法,十分感谢!!!
$sql = "select ".impload(',',$testa)." ...";
header("content-type:text/html; charset=UTF-8");@mysql_connect("localhost", "root","ghxwjszc") //选择数据库之前需要先连接数据库服务器
or die("数据库服务器连接失败");
@mysql_select_db("workshift") //选择数据库mydb
or die("数据库不存在或不可用");
//获取Cookie
$username = $_COOKIE['username'];
//执行SQL语句获得userflag的值$query3 = @mysql_query("set names gb2312");$query0 = @mysql_query("select userflag from technician "
."where username = '$username'")
or die("SQL语句执行失败");
//判断用户是否存在,密码是否正确
if($row0 = mysql_fetch_array($query0))
{
//session_start(); //标志Session的开始if($row0['userflag'] == 1)
{
$query1 = @mysql_query("select DISTINCT clientname from jobrecord")
or die("SQL语句执行失败1");$query2 = @mysql_query("select DISTINCT techname from jobrecord")
or die("SQL语句执行失败2");
if( isset( $_POST['submit']) ){
$post_arr = $sql_row_key = $sql_row_val = array();
$post_arr = $_POST['$post_data'];
if( count( $post_arr ) > 0 ){
foreach( $post_arr as $k=>$v ){
if( !empty( $v ) ){ //非空
$sql_row_key[] = $k; //查询列组成的数组
$sql_row_val[] = $k."='".$v."'"; //列对应的值组成的数组
}
}
if( count( $sql_row_key ) > 0 ){
$key_str = implode( ',', $sql_row_key);
$val_str = implode( ' AND ', $sql_row_val);
echo $sql = "SELECT $key_str FROM TABLE WHERE 1 $val_str ";
}
}
/*else{
echo '<font color="red">没有选择任何查询条件</font>';
}*/
} }else
{
echo "<script language='javascript'>window.alert('用户权限不正确')</script>";
echo "<script>window.location.href='http://localhost/logout1.php'</script>";
}
}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD> <BR><BR><BR><CENTER><H1>任务跟踪查询页面</H1></CENTER>
<TITLE> Administrator </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD> <body>
<center><form method="post" action="adminquery.php" name="send" onSubmit="return Check()">
<td width="210" class="p11" valign="bottom"><font color="#FF6699">请甄选查询条件</font></td>
<p>
客户名单:
<select name="clientname">
<option value="">客户名称</option>
<option value="all">分配的客户</option>
<?php
while($row1 = mysql_fetch_array($query1)){
?>
<option value="<?php echo($row1['clientname']); ?>">
<?php echo($row1['clientname']); ?>
</option>
<?php } echo'</select>'; ?>
</select>
<font color="#FF6699">*</font>
</p> <p>
技术工程师:
<select name="techname">
<option value="">技术负责人姓名</option>
<option value="all">分配的技术人员</option>
<?php
while($row2 = mysql_fetch_array($query2)){
?>
<option value="<?php echo($row2['techname']); ?>">
<?php echo($row2['techname']); ?>
</option>
<?php } echo'</select>'; ?>
</select>
<font color="#FF6699">*</font>
</p>
<p>当前任务状态:
<select name="currentstatus">
<option value="">任务状态</option>
<option value="all">当前所有状态</option>
<option value="售前支持">售前支持</option>
<option value="售前支持完成">售前支持完成</option>
<option value="测试">测试</option>
<option value="测试完成">测试完成</option>
<option value="开通运营">开通运营</option>
<option value="退单">退单</option>
</select> <font color="#FF6699">*</font> </p> <p>查询起始日期:
<input type="date" id="d1" name="time1" size="20" onFocus="javascript:show_cele_date(d1,'','',d1)">
</p>
<p>查询终止日期:
<input type="date" id="d2" name="time2" size="20" onFocus="javascript:show_cele_date(d2,'','',d2)">
</p> <p>查询任务初始状态记录:
<input type="checkbox" id="initialstatus" name="$post_data[initialstatus]" value="initialstatus">
</p> <p>查询任务分发日期记录:
<input type="checkbox" id="createddate" name="$post_data[createddate]" value="createddate">
</p> <p>查询任务跟进信息记录:
<input type="checkbox" id="recordcontent" name="$post_data[recordcontent]" value="recordcontent">
</p> <p>查询任务带宽峰值记录:
<input type="checkbox" id="topbandwidth" name="$post_data[topbandwidth]" value="topbandwidth">
</p>
<p>
<input type="submit" value="启动查询" >
<input type="reset" value="重置条件">
</p>
</form> </center>
<script src="Mymodify.js"></script>
<script language="javascript">
function Check()// 验证表单数据有效性的函数
{
if (document.send.clientname.value=="")
{
window.alert('请选择客户名单');
return false;
}
if (document.send.techname.value=="")
{
window.alert('请选择技术负责人');
return false;
} if (document.send.currentstatus.value=="")
{
window.alert('请选择查询状态!');
return false;
}
if (document.send.time1.value!="" && document.send.time2.value!="" && document.send.time1.value > document.send.time2.value)
{
window.alert('查询起始时间点应早于查询终止时间点!');
return false;
}
return true;
}
</script>
</body>
</html>check box name = name="$post_data[recordcontent]"
拿在下一页假如写$testa=$_POST['']; 请问是直接复制 checkbox name 到两单引号之间么?
$sql1 = mysql_query("select B.clientname,B.techname,B.currentstatus $key_str ".$initialstatus." $key_str ".$topbandwidth." $key_str ".$createddate." $key_str ".$recordcontent." from task A,jobrecord B where B.clientname = '$clientname' and B.techname = '$techname' and B.currentstatus = '$currentstatus' and A.clientname=B.clientname and A.techname=B.techname");echo"<center><table><tr><td>客户名称</td><td>技术负责人</td><td>任务历史状态记录</td><td>$w</td><td>任务历史记录日期</td><td>$z</td><td>$x</td><td>$y</td></tr>";////////////////////////////resource 结果输出//////////////////////////////////
echo "sql1:$sql1";
/////////////////////////////////////////////////////////////////////////////////
while ($rs1=mysql_fetch_object($sql1))
{
echo '<tr><td>'.$rs1->clientname.'</td><td>'.$rs1->techname.'</td><td>'.$rs1->currentstatus.'</td><td>'.$rs1->initialstatus.'</td><td>'.$rs1->recorddate.'</td><td>'.$rs1->createddate.'</td><td>'.$rs1->recordcontent.'</td><td>'.$rs1->topbandwidth.'</td></tr>'; }
echo'</table></center>';结果显示:客户名称 技术负责人 任务历史状态记录 任务历史记录日期
KFC will 测试
KFC will 测试
KFC will 测试
KFC will 测试
KFC will 测试
KFC will 测试 没有取到勾选的值,全部为空了
很抱歉,现在的问题主要是4个复选框的的value取不到,命名的name="$post_data[createddate]" value="createddate",我想勾选将value值post到下一页,并将变量带到SELECT语句里作为一个查询字段,我应该怎么改呢?
$box1=isset($_POST['box1'])?$_POST['box1']:'';
$box2=isset($_POST['box2'])?$_POST['box2']:'';
$box3=isset($_POST['box3'])?$_POST['box3']:'';
$sql="insert into `slselina` (tit) values ('".$box1."-".$box2."-".$box3."')";//合并存入数据库要调用的时候:
$sesql='select tit from slselina';
$res=$db->getRow($sesql);
explode('-',$res);//再分开来
你好,我将复选框放在了PHP标签下,仍然没有将VALUE值取到... 还要麻烦各位帮帮忙!
客户名称 技术负责人 任务历史状态记录 任务历史记录日期
KFC will 测试
KFC 王洲 测试
KFC 王洲 测试
KFC 王洲 测试
KFC 王洲 测试
KFC 王洲 测试
我理解你需要的是这个:<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
echo '你选中的字段:' . join(',', $_POST['$post_data']);
?>
<form method=post>
<p>查询任务初始状态记录:
<input type="checkbox" id="initialstatus" name="$post_data[initialstatus]" value="initialstatus">
</p>
<p>查询任务分发日期记录:
<input type="checkbox" id="createddate" name="$post_data[createddate]" value="createddate">
</p>
<p>查询任务跟进信息记录:
<input type="checkbox" id="recordcontent" name="$post_data[recordcontent]" value="recordcontent">
</p>
<p>查询任务带宽峰值记录:
<input type="checkbox" id="topbandwidth" name="$post_data[topbandwidth]" value="topbandwidth">
</p><input type=submit value=ok>
</form>
看看是不是你要的
现在是多个POST到的内容在select语句里用逗号分开,这样如果POST为空,那么select ,from .
select (POST 1),(POST 2) from ,就是这个(POST 1),(POST 2) 逗号的问题是吧
类似这样
$sql = "SELECT";
if(isset($_POST['a'])&&$_POST['a']!="")
{
$sql.= "{$_POST['a']}";
}
if(isset($_POST['b'])&&$_POST['b']!="")
{
$sql.= "{$_POST['b']}";
}
$sql .= FROM TABLENAME WHERE onditions
$query = mysql_query($sql);
这样能把你要求的条件都写到里面去.好像不难,思路上挺简单的,
也可以先对变量进行整理
例如
$a = isset($_POST['a'])?$_POST['a']:"一个无关紧要的值";
再写到SQL语句里面去就不会出现这样的事情了
而且可以全部查询出来不显示出来也可以吧.你的代码很乱,我没怎么看懂.
你好,根据你的建议,尝试了一下,代码如下if( isset( $_POST['submit']) ){
$post_arr = $sql_row_key = $sql_row_val = array();
$post_arr = $_POST['post_data']; //用这个来获取值(前一个页面传过来的值)
if( count( $post_arr ) > 0 ){ //判断前一个页面是否有选中多选框(如果有则执行下面程序)
foreach( $post_arr as $k=>$v ){
if( !empty( $v ) ){ //非空
$sql_row_key[] = $k; //查询列组成的数组
if( $v != '' ){ //如果不等于空,列有赋值
$sql_row_val[] = $k."='".$v."'"; //列对应的值组成的数组
}
}
}
if( count( $sql_row_key ) > 0 ){
$key_str = implode( ',', $sql_row_key); //这个变量是要查询的列
$val_str = implode( ' AND ', $sql_row_val); //这个是列所对应的值
if( $val_str){
$val_str = ' AND '.$val_str;
}
echo $sql = "SELECT $key_str FROM TABLE WHERE 1 $val_str ";
}
}
//else{
//echo '<font color="red">没有选择任何查询条件</font>';
//}
}
echo "<BR> <font color='#FF6699'>历史任务查询结果如下:</font>";$clientname=$_POST['clientname'];
$techname=$_POST['techname'];
$currentstatus=$_POST['currentstatus'];
$time1=$_POST['time1'];
$time2=$_POST['time2'];
if($key_str[$k]='recordcontent' && $sql_row_val[$v]='recordcontent')
{
$x="任务信息跟进记录";$x1='recordcontent';
//echo $x;
}
else{$x==''; $x1='';}
if($key_str[$k]='topbandwidth' && $sql_row_val[$v]='topbandwidth')
{
$y="任务带宽峰值记录";$y1='topbandwidth';
//echo $y;
}
else{$y==''; $y1='';}
if($key_str[$k]='createddate' && $sql_row_val[$v]='createddate')
{ $z="任务初始记录日期";$z1='createddate';
//echo $z;
}
else{$z==''; $z1='';}
if($key_str[$k]='initialstatus' && $sql_row_val[$v]='initialstatus')
{
$w="任务初始状态记录";$w1='initialstatus';
//echo $w;
}
else{$w=='';$w1=''; } /* if($clientname=='all' && $techname=='all')
{
$sqla=mysql_query("select ".$createddate." from task");
$sqlb=mysql_query("select ".$initialdate." from task");
}
if($clientname!='all' && $techname!='all')
{
$sqla=mysql_query("select ".$createddate." from task where clientname='$clientname' and techname='$techname'");
$sqlb=mysql_query("select ".$initialstatus." from task where clientname='$clientname' and techname='$techname'");
}
if($clientname=='all' && $techname!='all')
{
$sqla=mysql_query("select ".$createddate." from task where techname='$techname'");
$sqlb=mysql_query("select ".$initialstatus." from task where techname='$techname'");
}
if($clientname!='all' && $techname =='all')
{
$sqla=mysql_query("select ".$createddate." from task where clientname='$clientname'");
$sqlb=mysql_query("select ".$initialstatus." from tassk where clientname='$clientname'");
}
*/
//$rssqla=mysql_fech_array($sql_row_val);
//echo ($sql_row_val['createddate']->createddate);
echo $key_str;/////////time condition 1, both null
if ($time1=='' && $time2=='')
{if ($clientname!='all' && $techname!='all' && $currentstatus!='all')
///////////////////////where A,B,C
{//$sql1 = mysql_query("select B.clientname,B.techname,B.currentstatus,B.recorddate ".$key_str." from task A,jobrecord B where B.clientname = '$clientname' and B.techname = '$techname' and B.currentstatus = '$currentstatus' and A.clientname=B.clientname and A.techname=B.techname");
$sql1="select B.clientname,B.techname,B.currentstatus,B.recorddate,"; $sql1.= "{$x1}";
$sql1.= "{$y1}";
$sql1.= "{$z1}";
$sql1.= "{$w1}";
$sql1 .= "from task A,jobrecord B where B.clientname = '$clientname' and B.techname = '$techname' and B.currentstatus = '$currentstatus' and A.clientname=B.clientname and A.techname=B.techname;"
$query1 = mysql_query($sql1);
echo"<center><table><tr><td>客户名称</td><td>技术负责人</td><td>任务历史状态记录</td><td>$w</td><td>任务历史记录日期</td><td>$z</td><td>$x</td><td>$y</td></tr>";
while ($rs1=mysql_fetch_object($query1))
{
echo '<tr><td>'.$rs1->clientname.'</td><td>'.$rs1->techname.'</td><td>'.$rs1->currentstatus.'</td><td>'.$rs1->initialstatus.'</td><td>'.$rs1->recorddate.'</td><td>'.$rs1->createddate.'</td><td>'.$rs1->recordcontent.'</td><td>'.$rs1->topbandwidth.'</td></tr>'; }
echo'</table></center>';报错在这一行:$query1=mysql_query($sql1);
查询执行不正确,你把sql语句输出下 在数据库中试一下可不可以查询到