PHP, MYSQL数组从表单复选框到变量


PHP, MYSQL array from form checkboxes into variables

我创建了一个基于sql查询的有多个复选框的表单,所以结果值通常总是不同的

<input type="checkbox" name="warrior[]" value="<? echo $warrior_id; ?>">

但是我不知道如何从数组中获取用户选中的框的值。每个复选框都有我的数据库中的一行的id。

我需要运行在另一个查询中检查的每个id,以提供脚本其余部分所需的更多详细信息。

"SELECT col1, col2 FROM table WHERE id='$???? . '"

数组中最多有10个值(最多有10个复选框)

谢谢你的建议

在HTML中

<input type="checkbox" name="warrior[]" value="<? echo $warrior_id; ?>">

在PHP中-你可以内爆检查值数组。并检查IN在你的数据库

$getvalues = $_GET['warrior']; //only for example i am using this. you have to filter and do validation on it.
$getImplodeValue = implode(',', $getvalues);
$sqlQuery = "SELECT col1, col2 FROM table WHERE id IN '$getImplodeValue'";
  1. 检查验证

2。然后在下面使用:

$getWarrior = $_REQUEST['warrior'];  //get value 
$arrWarrior = '';
if(!empty($getWarrior))
{          
  $arrWarrior = implode(',', $getWarrior);
}
$sqlQuery = "SELECT col1, col2 FROM table WHERE 1";
if(!empty($arrWarrior))
{
   $sqlQuery .= " and id IN ($getImplodeValue)";
}