我正在尝试从 db WHERE 条件是一个数组中选择产品。我的方法在声明数组时有效,但在提交表单后生成数组元素时无效。
$selected_categories = array(); //create empty array
if(isset($_POST['submit'])){ //if form is submitted
$name = $_POST['category'];
foreach ($name as $category){
$selected_categories[] = $category; //add all checked checkboxes values into the array, every single $category element is text i.e. programming science
}
$get_products = "SELECT P.*, C.category_name, GROUP_CONCAT(category_name SEPARATOR ', ') AS cat FROM products P NATURAL JOIN categories C NATURAL JOIN product_to_categories WHERE category_name IN ('$selected_categories') GROUP BY product_name ORDER BY 1 DESC LIMIT $start_from,$per_page";
}
问题(我认为)在数组中,因为当我使用数组中的一个元素时:
... WHERE category_name IN ('$selected_categories[1]')...
工作正常它也在使用时工作:
... WHERE category_name IN ('programming', 'science')...
繁华开满天机