php - Foreach loop full execution and stop the rest of the script -
i have product table checking quantity respective product id(s) valid or not..
this code snippet :
$pids = explode(',',$pid); /*in form of 2,3,4.....*/ /*$pid->product_id*/ $q = explode(',',$q_total); /*in form of 2,3,4.....*/ /*$q->quantity*/ /*checking start*/ foreach($pids $index => $ps){ $quants = $q[$index]; $sql = $stsp->query("select quantity product id='$ps'"); $row = $sql->fetch(pdo::fetch_assoc); $quantity_rem = $row['quantity']; if($quants > $quantity_rem){ $array = array(); $array['errquant'] = 'wrong_quant'; $array['error_pr'] = $ps; echo json_encode($array); exit; /*stop rest of code executing*/ } } /*rest of code outside loop*/
so here happening checks quantity ($quantity_rem
) table of product id , if quantity less quantity given ($q
), script stops , echo product id..
but have more 1 product .. it's not checking rest since whenever there fault stops , echo out. want check products , echo out product id(s) , stop rest of script outside loop..
help needed! thanks.
and please don't talk me sql injection because know vulnerable , take care of that..
try this:
$pids = explode(',',$pid); /*in form of 2,3,4.....*/ /*$pid->product_id*/ $q = explode(',',$q_total); /*in form of 2,3,4.....*/ /*$q->quantity*/ /*checking start*/ $errors = array(); foreach($pids $index => $ps){ $quants = $q[$index]; $sql = $stsp->query("select quantity product id='$ps'"); $row = $sql->fetch(pdo::fetch_assoc); $quantity_rem = $row['quantity']; if($quants > $quantity_rem){ $array = array(); $array['errquant'] = 'wrong_quant'; $array['error_pr'] = $ps; $errors[] = $array; } } echo json_encode($errors);
Comments
Post a Comment