Update database if data exists
<p>I have a database that contains two columns. The product's name and it's quantity.
I also have two forms, an input for the user to add his new product, and an input for him to add the quantity.
The other form is for updating the quantity when the product gets sold.</p>

<p>The issue that I am facing is when the user uses the second form in order to update the quantity of a specific product, all of the product's quantity inside the database get subtracted as well. Hope I explained my problem right.</p>

<p><strong>FULL CODE:</strong></p>

<pre><code>&lt;?php


class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }
}

$servername = "localhost";
$username = "root";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=program", $username, $password);
    $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "&lt;script&gt;alert('Connected successfully');&lt;/script&gt;"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e-&gt;getMessage();
    }

 if (isset($_POST['sub']) == 1) {
    $id2 = $_POST['id2'];
    $qty2 = $_POST['qty2'];
    $to = "xxxxxx";
    $subject = "New sale report";
    $header = "From The shop";

    $stmt = $conn-&gt;prepare("SELECT brand_id FROM brands WHERE brand_id = :id2");
    $stmt-&gt;bindParam(':id2', $id2);
    $stmt-&gt;execute();
    $result = $stmt-&gt;setFetchMode(PDO::FETCH_ASSOC);
    foreach(new TableRows(new RecursiveArrayIterator($stmt-&gt;fetchAll())) as $k=&gt;$v) { 
        $r = $v;
        $s = $r - $qty2;
        if ($stmt-&gt;rowCount() === 1) {
            echo $s;
            $sql_new = "UPDATE brands SET brand_qty='$s'";
            $conn-&gt;exec($sql_new);
        }
        echo "&lt;script&gt;alert('New record successfully updated.')&lt;/script&gt;";
    }
    $stmt_m = $conn-&gt;prepare("SELECT brand_id FROM brands");
    $stmt_m-&gt;execute();
    $results = $stmt_m-&gt;setFetchMode(PDO::FETCH_ASSOC);
    foreach ( new TableRows(new RecursiveArrayIterator($stmt_m-&gt;fetchAll())) as $t=&gt;$d ) {
        $body = "Item sold: ". $d ."\n\n Quantity sold: ". $s ." ";
    }
    //mail($to, $subject, $body, $header);
 }

 $id = $_POST['id'];
 $qty = $_POST['qty'];

 if (isset($_POST['submit']) == 1) {
    $stmt_mt = $conn-&gt;prepare("SELECT brand_id FROM brands WHERE brand_id = :id");
    $stmt_mt-&gt;bindParam(':id', $id);
    $stmt_mt-&gt;execute();


    if($stmt_mt-&gt;rowCount() === 1) {
        echo "&lt;script&gt;alert('exists in database.')&lt;/script&gt;";   
    } else {
        echo "&lt;script&gt;alert('adding hoodie to database.')&lt;/script&gt;";
        $sql = "INSERT INTO brands (brand_id, brand_qty) VALUES ('$id', '$qty')";
        $conn-&gt;exec($sql);    
        echo "&lt;script&gt;alert('New record inserted succesfully.')&lt;/script&gt;"; 
    }
 }



?&gt;

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="post"&gt;
    &lt;input type="text" placeHolder="Enter ID" name="id" /&gt;
    &lt;input type="number" placeHolder="Quantity" name="qty" /&gt;
    &lt;input name="submit" type="submit" value="submit" /&gt;
&lt;/form&gt;

&lt;form method="post"&gt;
    &lt;input type="text" placeHolder="Enter ID" name="id2" /&gt;
    &lt;input type="number" placeHolder="Number of sales" name="qty2" /&gt;
    &lt;input name="sub" type="submit" value="submit" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>