在查询中使用复选框的结果

时间:2014-01-03 00:33:15

标签: php arrays checkbox

我有一个搜索表单,用户可以在其中选择搜索条件。

<form action="show_time_search3.php" method="post">
    <p><strong>From Date</strong> (YYYY-MM-DD): <input type="datetime" name="day" size="10" maxlength="10" value="<?php if (isset($_POST['day'])) echo $_POST['day']; ?>" /></p>
    <p><strong>To Date </strong> (YYYY-MM-DD): <input type="datetime" name="day2" size="10" maxlength="10" value="<?php if (isset($_POST['day2'])) echo $_POST['day2']; ?>" /></p>  
    <p><strong>Type:</strong>&nbsp(Click on one or more)&nbsp
    <strong>CL&nbsp</strong><input type=checkbox name=type[] size="2" maxlength="2" value=CL>&nbsp &nbsp
    <strong>CO&nbsp</strong><input type=checkbox name=type[] size="2" maxlength="2" value=CO>&nbsp &nbsp
    <strong>CS&nbsp</strong><input type=checkbox name=type[] size="2" maxlength="2" value=CS>&nbsp &nbsp
    <p><strong>Student ID</strong>: <input type="text" name="studentid" size="4" maxlength="4" value="<?php if (isset($_POST['studentid'])) echo $_POST['studentid']; ?>"  /></p>
    <p><input type="submit" name="submit" value="Submit" /></p>
</form>

我希望在查询中包含名为type的复选框的结果,如下所示:

// Make the query:
$q = "SELECT (TIMEDIFF(endtime, startime)) as difference,
timeid, 
DATE_FORMAT(day, '%b %d, %Y') as dia, 
DATE_FORMAT(startime, '%r') as start, 
DATE_FORMAT(endtime, '%r') as end, 
type, 
studentid from time 
WHERE studentid='$id' AND type=$type AND day>='$day' and day<='$day2'";

$r = @mysqli_query ($dbc, $q); // Run the query.

我试过内爆,但得到转换数组错误消息。

我只想在我的查询中包含类型数组结果。

1 个答案:

答案 0 :(得分:0)

如果您希望用户能够选择多种类型

<input type="checkbox" name="type[]" value="CL" checked="checked" />

将导致$_POST['type'][0]存在值CL。

<input type="checkbox" name="type[]" value="CL" checked="checked" />
<input type="checkbox" name="type[]" value="CO" checked="checked" />

将导致$_POST['type'][0]='CL'$_POST['type'][1]='CO'

只要您检查isset($_POST['type'])以避免错误,就可以内爆阵列

$where="WHERE studentid='$id'
    AND type IN ('".implode("','", $_POST['type'])."')
    AND day>='$day' and day<='$day2'";

应该有效,给予

type IN ('CL', 'CO')

但你真的需要清理那些$ _POSTs

相关问题