I have list of parameters and their values. I want to build mysql update string based on whether values are present or not. One way to check using if condition like below
if (!empty($value['p_status']) || !empty($value['quantity'])){
$strSet = '';
if (!empty($value['p_status'])) {
$strSet .= "status='$value[p_status]',";
}
if (!empty($value['quantity'])) {
$strSet .= "amount='$value[quantity]',";
}
if (!empty($value['shipping_freight'])) {
$strSet .= "shipping_freight='$value[shipping_freight]',";
}
}
$strSet = trim($strSet,',');
db_query("update table1 set ".$strSet." where pr1=123");
But there can be lots of parameters. Is there is any better way to do it ?
foreach($value as $key =>$value)thenif (!empty($key))