0

例: ユーザーが製品名以外のすべてを入力します。

提供されているものを検索する必要があるため、この場合はすべてproductName=

この例は、入力の任意の組み合わせに適用できます。

これを行う方法はありますか?

ありがとう。

    $name = $_POST['n'];
    $cat = $_POST['c'];
    $price = $_POST['p'];

if( !($name) )
{
    $name = some character to select all?
}


$sql = "SELECT * FROM products WHERE productCategory='$cat' and   
productName='$name' and productPrice='$price' ";

EDIT
ソリューションは、攻撃から保護する必要はありません。具体的には、その動的な部分を見ています。

4

1 に答える 1

2

何かのようなもの

$where_array = array();

if( isset($_POST['n']) )$where_array[] = "productName = '{$_POST['n']}'";
if( isset($_POST['c']) )$where_array[] = "productCategory '{$_POST['c']}'";
if( isset($_POST['p']) )$where_array[] = "productPrice = '{$_POST['p']}'";

    $where_stmt = implode( ' and ', $where_array  );
    if( $where_stmt )  
    {
    $sql = "SELECT * FROM products WHERE $where_stmt ";
//run query
    }
于 2012-06-25T03:06:32.190 に答える