php - Multiplication data from another table in SQL
one text
I have two tables on SQL database, first table is barang, and second table is barang_masuk.
barang :
- id_barang
- nama_barang
- jenis_id
- satuan_id
- harga_barang
- stok
barang_masuk :
- id_barang_masuk
- tgl_masuk
- barang_id
- supplier_id
- jumlah_masuk
- total_harga
I want to inserting data to barang_masuk where total_harga result is obtained from harga_barang * jumlah_masuk.
I'm confused to get the value of total_harga.
this is my code :
function addBarangMasuk()
{
$barang_id = $this->input->post('barang_id');
$jumlah_masuk = $this->input->post('jumlah_masuk');
$total_harga = $this->db->query("SELECT id_barang, nama_barang, jenis_id, satuan_id, stok, harga_barang * $jumlah_masuk FROM barang WHERE id_barang='$barang_id'");
$data = [
'id_barang_masuk' => $this->kode_barang_masuk(),
'tgl_masuk' => $this->input->post('tgl_masuk'),
'barang_id' => $barang_id,
'supplier_id' => $this->input->post('supplier_id'),
'jumlah_masuk' => $jumlah_masuk,
'total_harga' => $total_harga
];
$this->db->insert('barang_masuk', $data);
$data_barang = $this->db->query("SELECT * FROM barang WHERE id_barang='$barang_id'")->result();
foreach ($data_barang as $db) {
$stok = $db->stok;
$sisa_stok = $stok + $jumlah_masuk;
}
$this->db->query("UPDATE barang SET stok='$sisa_stok' WHERE id_barang='$barang_id'");
}
Please help me, I am new to MySql
Source