0

At first select query if id exists just update quantity else execute insert(id, quantity) query in my case just update query executed

$fodid = $data->orfid;
$quantity = $data->orqnt; 

include('db.php');     

try{                              
    $sqlchk="SELECT `orfoodid` AS  FROM `orders` where `orfoodid`=':chkfoodid';";
    $sqlupdate="UPDATE `resturant`.`orders` SET `orfoodqnt` = ':chcount' WHERE `orders`.`orfoodid` = ':chfodid';";
    $sql="INSERT INTO `resturant`.`orders` (`orfoodid`, `orfoodqnt`) VALUES (:foodid,:count);";  

    for($i=0;$i<sizeof($fodid);$i++){

        $rs = $conn->prepare($sqlchk);
        $rs->execute(array(':chkfoodid'=>$fodid[$i]));       
        $result=$rs->fetch(PDO::FETCH_ASSOC);     

        if(!$result)
        {
            $rs = $conn->prepare($sqlupdate);
            $rs->execute(array(':chfodid'=>$fodid[$i],
                           ':chcount'=>$quantity[$i]));
            echo 'Order updated';                      
        } else {         
            $rs = $conn->prepare($sql);
            $rs->execute(array(':foodid'=>$fodid[$i],
                           ':count'=>$quantity[$i])); 
            echo 'Order Saved';
        }           

    }                          
    $conn=null;
}catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159

1 Answers1

0

i solved my problem it was syntax error ':chkfoodid' just remove single quotations :chkfoodid