Jump to content

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\AplikasiInventaris\function.php on line 186 Fatal error: Uncaught TypeError: Unsupported operand types: int + string in C:\xampp\htdocs\AplikasiInventaris\function.php:186 St


Recommended Posts

if(isset($_POST['masuk'])){

    $id_data_barang      = $_POST['id_data_barang'];

    $nama_barang         = $_POST['nama_barang'];

    $qty                 = $_POST['qty'];

    $tanggal_masuk       = $_POST['tanggal_masuk'];

    $nama_toko_barang    = $_POST['nama_toko_barang'];

    $nama_pegawai        = $_POST['nama_pegawai'];

 

    $query = mysqli_query($con,"INSERT INTO barang_masuk (id_data_barang, nama_barang, qty, tanggal_masuk, nama_toko_barang, nama_pegawai) values ('$id_data_barang','$nama_barang','$qty','$tanggal_masuk','$nama_toko_barang','$nama_pegawai')");

 

    $query2 = mysqli_query($con, "SELECT * FROM data_barang WHERE data_barang.id_data_barang = '$id_data_barang'");

    $data = mysqli_fetch_array($query2);

    $stok_awal = $data['stock'];

    $stok_akhir1 = $stok_awal + $qty ;

    $qty = $qty;

    $query3 = mysqli_query($con, "UPDATE data_barang SET stock = '$stok_akhir1' WHERE data_barang.id_data_barang = '$id_data_barang' ");

 

    if($query){

        header('location:barang_masuk.php');

    } else {

        echo '

        <script>alert("Gagal Menambahkan");

        window.location.href=barang_masuk.php

        </script>

        ';

    }

} else if(isset($_POST['editbarangmasuk'])) {

    // EDIT DATA PEMBELIAN

    $id_masuk = $_POST['id_masuk'];

    $id_data_barang = $_POST['id_data_barang'];

    $qty = $_POST['qty'];

 

    // TRIGGER EDIT STOK BARANG

    $query2 = mysqli_query($con, "SELECT * FROM data_barang WHERE data_barang.id_data_barang = '$id_data_barang'");

    $data = mysqli_fetch_array($query2);

    $stok_awal = $data['stock'] ?? 0;

    // $stok_akhir1 = $stok_awal + $jumlah_pembelian;

   

    $query = "SELECT * FROM barang_masuk WHERE barang_masuk.id_masuk = '$id_masuk' ";

    $sql_pm = mysqli_query($con, $query) or die (mysqli_error($con));

    $data = mysqli_fetch_array($sql_pm);

    $stok_awal = $data['qty'] ?? 0;

   

    $stok = ($stok_awal - $data['qty']) + $qty;

    $queryz = mysqli_query($con, "UPDATE barang_masuk SET qty='$qty' WHERE id_masuk ='$id_masuk'");

    $query5 = mysqli_query($con, "UPDATE data_barang SET stock = '$stock' WHERE data_barang.id_data_barang = '$id_data_barang'");

    // END TRIGGER EDIT STOK BARANG

    if($queryz){

        // BERPINDAH HALAMAN

        header('location:barang_masuk.php');

    } else {

        echo '

        <script>alert("Gagal Update");

        window.location.href=barang_masuk.php

        </script>

        ';

    }

}

8 hours ago, maxxd said:

Is there a question here?

This is my question help please 

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\AplikasiInventaris\function.php on line 186

Fatal error: Uncaught TypeError: Unsupported operand types: int + string in C:\xampp\htdocs\AplikasiInventaris\function.php:186 St

17 hours ago, ginerjm said:

Can you point to the line that the message is mentioning?

And what does "$qty = $qty" do for you?

$query = "SELECT * FROM barang_masuk WHERE barang_masuk.id_masuk = '$id_masuk' ";

    $sql_pm = mysqli_query($con, $query) or die (mysqli_error($con));

    $data = mysqli_fetch_array($sql_pm);

  Line  184. $stok_awal = $data['qty'] ?? 0;

   

Line  186.  $stok = ($stok_awal - $data['qty']) + $qty;

    $queryz = mysqli_query($con, "UPDATE barang_masuk SET qty='$qty' WHERE id_masuk ='$id_masuk'");

    $query5 = mysqli_query($con, "UPDATE data_barang SET stock = '$stock' WHERE data_barang.id_data_barang = '$id_data_barang'");

On 1/3/2022 at 3:43 AM, sandi123 said:

$qty = $_POST['qty'];

The POST'ed values are always Strings. 

2 hours ago, sandi123 said:

Line  184. $stok_awal = $data['qty'] ?? 0;

Line  186.  $stok = ($stok_awal - $data['qty']) + $qty;

$stok_awal is whatever numeric value comes back in the qty field (we have to assume that this is numeric because, coming from the database, it really ought to be) or zero, if the retrieved value was NULL. 

$qty is still the String value from the POST'ed data values. 

[int] + [string] => Error. 

First validate that the POST'ed value really is [the String representation of] a numeric value and then convert that value to the correct Data Type before using it.

Regards, 
   Phill  W.

 

First and foremost, I hope this code isn't on a live server - you're wide open to SQL injection in many, many places.

That having been said, there's also some suspect logic - for instance, line 186. Even if it were working I'm not sure you'd be getting the results you expect. Right before that, you set $stok_awal to the value of $data['qty'] or 0 if $data['qty'] is null. You then subtract $data['qty'] from $stok_awal (which again, is the value of $data['qty'] or 0) without checking to make sure that $data['qty'] exists.

The basic problem here is that $data['qty'] doesn't exist. Or more to the point, $data is null. You need to output $data and see what exactly - if anything - it contains.

After that, look into prepared statements and update all your queries.

  • Great Answer 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.