Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于复选框在PHP中运行SQL查询_Php_Sql - Fatal编程技术网

基于复选框在PHP中运行SQL查询

基于复选框在PHP中运行SQL查询,php,sql,Php,Sql,我正在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); 但是我希望能够添加复选框来修改查询,而无需刷新页面

我正在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" />
如果同时选中值为“已提交”和“已接受”的输入,则查询将为:

SELECT * from porting where status = 'Submitted' or status = 'Accepted'

您的复选框应该使用数组语法

<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" />
警告:上面的SQL查询易受攻击,但它应该会让您走上正确的道路

$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”

您已经告诉我们您想要什么。现在告诉我们您尝试了什么。您所说的“不刷新页面”是什么意思?
按字段排序(状态为“已提交”、“已拒绝”、“已取消”、“已接受”)
?好奇。这如何处理状态为“已提交”或状态为“已接受”的
?更具体地说,是
子句。你在
中的
条款是否会这样做?@Fred ii-我认为这个答案是在冒昧地假设,
的使用不是严格要求的,因此
中的
可能是一个合适的替代品。@PatrickQ刚刚陈述了OP在问题中提出的问题如果同时选中了值为“已提交”和“已接受”的输入,则查询将是:
SELECT*from porting,其中status='Submitted'或status='Accepted'
“@Fred ii Yes”IN”是最佳选择,因为它使您的答案动态而非具体。此外,“IN”中的“是否与您认为需要相同”或“@Pinu谢谢你为我澄清这一点。我喜欢了解事情的运作方式,干杯。
$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;
select * from porting where status = 'Submitted' or status = 'Cancelled'