Using mysqli prepared statement to prevent SQL injection, how to dynamically bind SQL statement?
There a few combinations of the SQL that concerned: If user select one or more items, the SQL string will use IN clause, if user did not select any item (empty), the SQL string will use LIKE clause:
Condition 1
select category,uom,product
from tbl_product
where category like '%'
and uom like '%'
and product like '%'
Condition 2
select category,uom,product
from tbl_product
where category in ('Soft Drinks','Liquor')
and uom in ('Can','Bottle')
and product in ('Pepsi','Coca Cola','Budweiser')
Condition 3
select category,uom,product
from tbl_product
where category in ('Soft Drinks','Liquor')
and uom in ('Can','Bottle')
and product like '%'
PHP Code
For single value I am using below code
$sql_product = $conn->prepare("select * from tbl_product where category = ?");
$sql_product->bind_param('s', $category);
$sql_product->execute();