将$ _POST数据放入数组中

I try to put all data from $_POST into an Array like this:

$produk =
        [
            'kode'=>$_POST['kode'],
            'id_kategori'=>$_POST['kategori'],
            'produk'=>$_POST['produk'],
            'keterangan'=>$_POST['keterangan'],
            'harga'=>$_POST['harga'],
            'potongan'=>$_POST['potongan'],
            'jumlah'=>$_POST['jumlah'],
            'berat'=>$_POST['berat'],
            'warna'=>$_POST['warna'],
            'ukuran'=>$_POST['ukuran']
        ];

but i always got an Error when i try to insert it into some Class and got this kind of error:

( ! ) Fatal error: Uncaught Error: Call to a member function tambahProduk() on array in C:\wamp64\www\core\test.php on line 35
( ! ) Error: Call to a member function tambahProduk() on array in C:\wamp64\www\core\test.php on line 35

it work fine when i test my data using this:

$data =
        [
            'kode' => 'abcde',
            'id_kategori' => '1',
            'produk' => 'Topi',
            'keterangan' => 'Baju ini dibuat dengan penuh hati.',
            'harga' => '250000',
            'potongan' => '0',
            'jumlah' => '3',
            'berat' => '100',
            'warna' => 'merah, kuning, hijau',
            'ukuran' => 's, m, xl, xxl'
        ];

When i using Print_r on $produk i got this Result:

Array ( [kode] => JKW001 [id_kategori] => 1 [produk] => Baju Jokowi 01 [keterangan] => Kaos oblong enak sekali dipakai [harga] => 10000 [potongan] => 2 [jumlah] => 12 [berat] => 11 [warna] => merah, kuning, hijau, biru, coklat [ukuran] => A,B,C,D,E,F,G ) 

here what inside tambahProduct function:

public function tambahProduk($produk){
        try{
            $this->pdo->beginTransaction();
            $sql = 'INSERT INTO produk 
            (kode_produk, id_kategori, nama_produk, keterangan, harga, potongan, jumlah_tersedia, berat)
            VALUES (:kode_produk, :id_kategori, :nama_produk, :keterangan, :harga, :potongan, :jumlah_tersedia,
          :berat)';

            $stmt = $this->pdo->prepare($sql);
            $stmt->bindParam(':kode_produk', $produk['kode'], \PDO::PARAM_STR);
            $stmt->bindParam(':id_kategori', $produk['id_kategori'], \PDO::PARAM_INT);
            $stmt->bindParam(':nama_produk', $produk['produk'], \PDO::PARAM_STR);
            $stmt->bindParam(':keterangan', $produk['keterangan'], \PDO::PARAM_STR);
            $stmt->bindParam(':harga',$produk['harga'] ,\PDO::PARAM_INT);
            $stmt->bindParam(':potongan', $produk['potongan'],\PDO::PARAM_INT);
            $stmt->bindParam(':jumlah_tersedia',$produk['jumlah'] ,\PDO::PARAM_INT);
            $stmt->bindParam(':berat',$produk['berat'] ,\PDO::PARAM_INT);
            $stmt->execute();
            $id_produk = $this->pdo->lastInsertId();

            $sql2 = 'INSERT INTO ukuran (ukuran) VALUE (:ukuran)';
            $stmt = $this->pdo->prepare($sql2);
            $ukuran = explode(",",$produk['ukuran']);
            $id_ukuran = [];
            foreach ( $ukuran as $key => $value) {
                if (!empty($value)) {
                    $stmt->bindParam(':ukuran', $value);
                    $stmt->execute();
                    $id_ukuran[] = $this->pdo->lastInsertId();
                    //echo $value;
                    //print_r($id_ukuran);
                }
            }

            $sql3 = 'INSERT INTO ukuran_produk (id_produk, id_ukuran) VALUES (:id_produk,:id_ukuran)';
            $stmt = $this->pdo->prepare($sql3);
            foreach ( $id_ukuran as $key => $value) {
                if (!empty($value)) {
                    $stmt->bindParam(':id_produk', $id_produk);
                    $stmt->bindParam(':id_ukuran', $value);
                    $stmt->execute();
                }
            }

            $sql4 = 'INSERT INTO warna (warna) VALUE (:warna)';
            $stmt = $this->pdo->prepare($sql4);
            $warna = explode(",",$produk['warna']);
            $id_warna = [];
            foreach ( $warna as $key => $value) {
                if (!empty($value)) {
                    $stmt->bindParam(':warna', $value);
                    $stmt->execute();
                    $id_warna[] = $this->pdo->lastInsertId();
                    //echo $value;
                    //print_r($id_warna);
                }
            }

            $sql5 = 'INSERT INTO warna_produk (id_produk, id_warna) VALUES (:id_produk,:id_warna)';
            $stmt = $this->pdo->prepare($sql5);
            foreach ( $id_warna as $key => $value) {
                if (!empty($value)) {
                    $stmt->bindParam(':id_produk', $id_produk);
                    $stmt->bindParam(':id_warna', $value);
                    $stmt->execute();
                }
            }

            $this->pdo->commit();
        }catch (\PDOException $e){
            echo $e;
        }

    }

so what can causing those error although the data is a same. Thanks.

I suspect it may be to do with $_POST for your INT values being passed as a string. So when your binding $_POST data to PDO as an INT it's failing.

An example of this below:

$_POST['test'] = '0';

if (filter_var($_POST['test'], FILTER_VALIDATE_INT)) {

    echo 'True';

} else {

    echo 'False';

}

Output:

False

I'd recommend that you cast your $_POST values to an INT using ternary like below:

$_POST['potongan'] = '0';

$pontogan = (is_numeric($_POST['potongan']) ? (int)$_POST['potongan'] : NULL);

So if $_POST['potongan'] is numeric then cast $_POST['potongan'] as INT and assign to $pontogan else $pontogan will be assigned NULL.

Finally, let's summarize our discussion in the comments giving other people a chance to get a quick overview of what happened here:

The error Call to a member function tambahProduk() on array is not caused by the data format: $data and $produk are defined in the same way.

So, the function call is the problem: From the function definition, it became obvious that the function is defined inside a class. It turned out that the instance of this class was saved to $produk. The testing call with $data works so far: $produk->tambahProduk($data);

The call with the real data from $_POST doesn't work anymore. From the initial question it can be seen, that this data is also stored in $produk. So the function call would be: $produk->tambahProduk($produk);

And here we found the problem: In $produk, the data array from $_POST is stored. So there isn't any object anymore. That also means that the desired object's member function tambahProduk() can't be called anymore.

And what about the error message?

It says, there was a function call on array. That's because the data array was found inside of $produk. It should have been an object which has a function tambahProduk().