我在PHP中运行这个SQL查询:
$stmt = $pdo_conn->prepare("SELECT * from porting order by field(status, 'Submitted', 'Rejected', 'Cancelled', 'Accepted') ");
$stmt->execute(array());
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);但我希望能够添加复选框来修改查询,而无需刷新页面
例如,
<input type="checkbox" name="status" value="Submitted" />
<input type="checkbox" name="status" value="Rejected" />
<input type="checkbox" name="status" value="Cancelled" />
<input type="checkbox" name="status" value="Accepted" />因此,如果检查了值为“Submitted”的输入,则查询将更改为:
SELECT * from porting where status = 'Submitted'如果同时选中了值为“Submitted”和“Accepted”的输入,则查询将为:
SELECT * from porting where status = 'Submitted' or status = 'Accepted'发布于 2014-04-30 22:25:34
复选框应使用数组语法
<input type="checkbox" name="status[]" value="Submitted" />
<input type="checkbox" name="status[]" value="Rejected" />
<input type="checkbox" name="status[]" value="Cancelled" />
<input type="checkbox" name="status[]" value="Accepted" />从那里开始,你就是implode。
$sql = "SELECT * from porting where status IN('".implode("', '", $_POST['status'])."')";警告:上面的SQL查询容易受到 的攻击,但它应该会让您走上正确的道路。
发布于 2014-04-30 22:34:05
$checkboxes = $_POST['status'];
if(count($checkboxes)) {
$where_clause = ' where';
}
else {
$where_clause = '';
}
foreach($checkboxes as $el) { $el = addslashes($el);
$where_clause .= " status = '$el' or";
}
$where_clause = substr($where_clause, 0, -2);
$query = 'select * from porting' . $where_clause;最后,$query将包含如下字符串:
select * from porting where status = 'Submitted' or status = 'Cancelled'如果你想发送多个值,你应该使用name="status[]"而不是name="status"。
https://stackoverflow.com/questions/23390592
复制相似问题