dimanche 24 janvier 2016

how can i use condition checks in php using mysql PDO statements?

i am trying to simplify my code which has multiple checks.. following is my code -

$query2 = "SELECT * FROM sample.temp";
$rslt = $dbo->query($query2);
if($rslt->rowCount() > 0)
{
    foreach($rslt as $item)
    {
        $id = $item['entry_id'];
        $Qty = $item['Qty'];
        $group_ID = $item['group_ID'];
        $component = $item['component'];
        $vendor_ID = $item['vendor_ID'];

        $stmt = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID=$vendor_ID AND component='$component' LIMIT 1");
        if ($stmt->execute(array($_GET['Qty'])))
            {
            while ($row = $stmt->fetch(PDO::FETCH_OBJ))
                {
                $Q = $row->Qty; 
                }
            }
        $query4 = "UPDATE sample.stock SET Qty=(Qty+$Q) WHERE stock_ID=$vendor_ID AND component='$component'";//updating single existing entries
            if ($dbo->query($query4))
            {echo "Production updated !";}
            else {echo "Production not updated!";}

        $stm = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID=$vendor_ID AND component!='$component' LIMIT 1");
        if ($stm->execute(array($_GET['Qty'])))
            {
            while ($row = $stm->fetch(PDO::FETCH_OBJ))
                {
                $Qt = $row->Qty; 
                }
        $query5 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qt)";//inserting new entry
            if ($dbo->query($query5))
            {echo "Production updated !";}
            else {echo "Production not updated!";}
    $st = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID!=$vendor_ID AND component='$component' LIMIT 1");
        if ($st->execute(array($_GET['Qty'])))
            {
            while ($row = $st->fetch(PDO::FETCH_OBJ))
                {
                $Qtt = $row->Qty; 
                }
        $query6 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qtt)";//inserting new entry
            if ($dbo->query($query6))
            {echo "Production updated !";}
            else {echo "Production not updated!";}
    }       
}

is there any way i can possibly reduce this code or change the way conditions are checked? If i could use if statements in all three select statements it would be great ! Thanks.

Aucun commentaire:

Enregistrer un commentaire