多选不起作用
编辑:我编辑我的问题,使其更清晰我希望。多选不起作用
我构建了一个带有多个搜索框的表单的页面,通过$ _GET的方法从表中的数据库表中搜索和显示值。它现在工作很好。
我需要什么:我想让一些搜索框允许多选,但我只得到一个选择的结果。
这是查询:
$query_Recordset2 = sprintf("SELECT * FROM satislar WHERE satis_satici LIKE %s AND urun_satilanurun LIKE
%s AND urun_pstnadslhizturu LIKE %s AND akt_aktalan LIKE %s AND satis_modem LIKE %s AND akt_aktdurum LIKE
%s AND akt_ttonayi LIKE %s AND satis_tarih between %s and %s ORDER BY satis_tarih DESC",
这是选择选项:
<select name="satici[]" size="4" multiple="multiple" id="satici" size="4" class="span2 nostyle chzn-select" style="margin-top:9px;width:150px;">
<option value="%">TÜM SATICILAR</option>
<?php
do {
?>
<option value="<?php echo $row_saticilistesi['satis_satici']?>"><?php echo $row_saticilistesi['satis_satici']?></option>
<?php
} while ($row_saticilistesi = mysql_fetch_assoc($saticilistesi));
$rows = mysql_num_rows($saticilistesi);
if($rows > 0) {
mysql_data_seek($saticilistesi, 0);
$row_saticilistesi = mysql_fetch_assoc($saticilistesi);
}
?>
我等待着您的宝贵意见。
使用多种选择你需要设置你的HTML这样的(这就像你说你做了它的声音:
<select name="foo[]" multiple="multiple">
<!-- options go here -->
</select>
,然后在你的PHP可以访问他们像这样的数组:
<?php
// This will print out all of the selected values.
print_r($_GET['foo']);
要在表格中回声这些结果:
<table>
<tr><th>Option</th></tr>
<?php foreach ($_GET['foo'] as $option) : ?>
<tr><td><?php echo htmlspecialchars($option) ?></td></tr>
<?php endforeach; ?>
</table>
我将您的代码添加到了我的页面顶部。该表是空的,但在顶部它说:“阵列([0] => HASAN MIZRAK [1] => SERDAR KAPLAN)” – user2490870
是的,这意味着HASAN MIZRAK和SERDAR KAPLAN是两个选择的选项,如果你想在表中回显结果看看我更新的帖子。 – chrislondon
是的,所选的选项列在表中,但这不是我所需要的。我需要在查询中使用它们,并将结果与这两位销售员进行过滤。 – user2490870
name="satici[]" multiple="multiple"
是COR rect HTML。
你想要什么在PHP做将是:
$arr_search = array();
foreach((array)@$_POST['satici'] as $h)
$arr_search[] = $h; // Escape this value (without "'")
$str_search = implode('|', $arr_search);
和查询内部,WHERE satis_satici REGEXP '{$str_search}'
取代例如WHERE satis_satici LIKE %s
。
请注意,col LIKE 'search'
通常等于col REGEXP 'search'
和col LIKE 'search' OR col LIKE 'search'
通常等于col REGEXP 'search|search2'
,这是我的示例提供的。
除此之外,我建议你尽可能地获取变量,列名等。维护起来变得容易很多,而且您只需要翻译成最终用户。
罗宾,感谢您的快速回复和建议。我会记住这一点。我试过你的代码,但它给了我这个错误:“得到错误”空(子)表达式“从正则表达式”。我把这个代码放在sql查询之前,并用你的代码改变了查询。 – user2490870
'print_r($ _ POST ['satici'])'给你什么? –
你的代码之前还是之后?我昨天试着用chrislondon的解决方案。这不是我需要的确切的东西,但它给了我在查询后选择的两个名字。但现在它再次给出同样的错误“得到错误”从正则表达式“空(子)表达式”。 – user2490870
你可能会发布这个HTML吗? – Bananam00n
你有没有听说过'$ _GET'或'$ _POST'? – bitWorking
https://dl.dropboxusercontent.com/u/5326510/code.html – user2490870