使用表单数据作为输入的条件查询
我正在尝试使用 PHP 和 mongodb 使用表单中的输入执行查询。
<form method="post" action="search.php">
<tr><td>Firm</td><td><input type="text" name="firm" /></td></tr>
<tr><td>City</td><td><input type="text" name="city" /></td></tr>
<tr><td>State</td><td><input type="text" name="state" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Search" /></td></tr>
</form>
如果所有表单字段都已填写,我可以轻松完成此操作(在 search.php 页面上)
extract($_POST);
$query = array("firm" => $firm, "city" => $city, "state" => $state);
$fields = array("firm");
$cur = $collection->find($query,$fields);
但是如果一个或多个字段为空怎么办?是否有一种简单的方法来构造查询,以便仅包含非空字段?
谢谢,
麦克德莫特
I'm trying to execute a query using PHP and mongodb using inputs from a form.
<form method="post" action="search.php">
<tr><td>Firm</td><td><input type="text" name="firm" /></td></tr>
<tr><td>City</td><td><input type="text" name="city" /></td></tr>
<tr><td>State</td><td><input type="text" name="state" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Search" /></td></tr>
</form>
If all of the form fields are filled, I can do this easily (on the search.php page)
extract($_POST);
$query = array("firm" => $firm, "city" => $city, "state" => $state);
$fields = array("firm");
$cur = $collection->find($query,$fields);
But what if one or more of the fields is empty? Is there an easy way to construct the query such that only non-empty fields are included?
Thanks,
mcdermott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
这样您只需确保表单字段名称与数据源的字段名称相同。当然,您可以在循环遍历已验证的数组之前进行验证。
What about:
This way you just have to make sure that the form field names are the same as the field names of your data source. Of course you can do the validation before you loop over the already validated array.