updating database where had the same name but when it has new name will make new data [duplicate]

I want to update my data at database where condition that they had same name they will update the stock not making new data.

But when U run this program nothing happen and I got no error.

Please help me to fix it.

This is my query code to update data while button clicked.

if (isset($_POST['tambah'])) {
        $namaBarang = $_POST['barang'];
        $stok = $_POST['stok'];

        //cek barang sudah ada atau belum
        $result = mysqli_query($conn,"SELECT*FROM barang WHERE name ='$namaBarang'");
        if (mysqli_fetch_assoc($result)) {
            mysqli_query($conn,"UPDATE barang SET stock = stock + $stok WHERE name = $namaBarang");
        } else{
            mysqli_query($conn,"INSERT INTO barang VALUES ('','$namaBarang',$stok)");
        }