1

我有SELECT name="idproduct"一个表格,它的选项值是我选择的产品的 ID。我想用我一个接一个选择的产品的 ID 在 PHP 中创建一个数组。
例子:

$idproducts=(1, 2, 14, 24, 8)

在表的顶部,我有 SELECT,首先我选择一种产品。然后我选择另一个等等......我需要在该选择中选择产品的 ID,因为在页面下方我有一个表格,我想在顶部显示我选择的产品。

$sql='SELECT    id_product,
        nameproduct,
        situation,
        quantity            
        FROM product
        WHERE id_product = "'.$idproducts.'"';

有人会解释我如何使用发布的 ID 制作数组SELECT

4

4 回答 4

1

$sql='SELECT id_product、nameproduct、情况、数量 FROM product WHERE id_product IN (.$idproducts.)';

于 2012-10-18T20:00:25.867 回答
0

假设您<select>是多选,您可以像这样循环遍历数组来构建查询字符串:

$sql='SELECT id_product,
        nameproduct,
        situation,
        quantity            
        FROM product
        WHERE 1';

foreach( $_POST['idproducts'] as $k=>$v ){
    $sql .= ' OR id_product = ' . $v;
}
于 2012-10-18T19:55:56.493 回答
0

这是我喜欢用来将数据数组转换为或基于 where 查询的函数。

    $sql_stub = "SELECT id_product, nameproduct, situation, quantity FROM product";
    $sql = orQuery($sql_stub, array("id1", "id2"), "id_product");

    function orQuery($sql_stub, $array, $field){

        //START WHERE CLAUSE
        $where = "WHERE ";

        //ADD DATA TO THE WHERE CLAUSE
        foreach($array as $data){
                    $where = $where." ".$field." = '".$data."' OR ";
        }

        //CHECK THAT THE WHERE CLAUSE IS MORE THAN JUST WHERE
        if($where!="WHERE "){
            //REMOVE THE TRAILING OR
            $where = substring($where, strlen($where)-4);

            //APPEND TO SQL_STUB
            $sql_stub = $sql_stub.$where;
        }
        return $sql_stub;
    }
于 2012-10-18T20:14:19.530 回答
0

如果 $idproducts 是一个数组:

$sql='SELECT id_product, nameproduct, situation, quantity FROM product 
      WHERE id_product IN ('.implode(',',$idproducts).')';
于 2012-10-18T20:05:56.757 回答