【发布时间】:2018-05-20 15:11:10
【问题描述】:
我想创建一个类似this post 的表单,但我想这样做,如果其中一个输入为空,那么 php 仍将处理查询。我使用INNERJOIN 还是LEFTJOIN?
编辑: 这是来自that post的html表单:
<form action="results.php" method="GET">
<input type="text" name="input">
<input type="text" name="topic">
<input type="text" name="location">
</form>
以及它的 php 代码:
$db = new mysqli(*your database connection information here*);
$input = $_GET['input']; //this is for the text input - ignore
$topic = $_GET['topic']; // the first select box value which works well
$location = $_GET['location']; //the second select box value which isn't being inserted into the query
$combined = $input . $topic . $location;
$terms = explode(" ", $combined);
$stmt = $db->prepare("SELECT * FROM search WHERE input = ? AND topic = ? AND location = ?");
$stmt->bind_param("sss", $input, $topic, $location);
$stmt->execute();
$stmt->close();
例如,如果“主题”输入为空,我想让SELECT 查询仍然返回一行,而不是什么都没有
【问题讨论】:
-
PHP 处理与
left或inner连接无关。请添加代码,以便我们查看您实际在做什么。 -
@user3783243 代码添加
-
所以您希望动态构建
where?这不是joins 的用途。您应该在 PHP 中使用条件来动态构建where子句。 -
使用
pdo而不是mysqli。它的占位符(:placeholder)格式对于此任务更加灵活。