Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
Undefined Index on Line 11, PHP MYSQL
Gambar errorSaya baru saja mempelajari PHP
dan berhasil membuat login, register, create, view. Tetapi ketika mencoba melakukkan upload gambar. Saya mendapatkan error yang seperti diatas. Ada yang bisa bantu?
Source Code:
<?php
include 'koneksi.php';
$nama_barang = $_POST['namabrg'];
$jumlah_barang = $_POST['jumlah'];
$brg_masuk = $_POST['brgmsk'];
$brg_bagus = $_POST['bagus'];
$brg_perbaikan = $_POST['perbaikan'];
$brg_rusak = $_POST['rusak'];
$brg_ket = $_POST['ket'];
$gambar = $_FILES['gambar']['name'];
if($gambar != "") {
$ekstensi_diperbolehkan = array('png','jpg');
$x = explode('.',$gambar);
$ekstensi = strtolower(end($x));
$file_tmp = $_FILES['gambar']['tmp_name'];
$angka_acak = rand(1,999);
$nama_gambar_baru = $angka_acak.'-'.$gambar;
if(in_array($ekstensi, $ekstensi_diperbolehkan) === true) {
move_uploaded_file($file_tmp, 'tambah/gambar/'.$nama_gambar_baru);
$query = "INSERT INTO barang (nama,jumlah,brg_msk,bagus,perbaikan,rusak,keterangan,gambar) values ('$nama_barang','$jumlah_barang',
'$brg_masuk','$brg_bagus','$brg_perbaikan','$brg_rusak','$brg_ket','$nama_gambar_baru')";
$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query gagal dijalankan: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {
echo "<script>alert('Data berhasil ditambah.');window.location='tambah.php';</script>";
}
} else {
echo "<script>alert('Ekstensi gambar yang diperbolehkan hanya JPG dan PNG');window.location='tambah.php';</script>";
}
} else {
$query = "INSERT INTO barang (nama,jumlah,brg_msk,bagus,perbaikan,rusak,keterangan,gambar) values ('$nama_barang','$jumlah_barang',
'$brg_masuk','$brg_bagus','$brg_perbaikan','$brg_rusak','$brg_ket',null)";
$result = mysqli_query($koneksi, $query);
if(!$result) {
die ("Query gagal dijalankan: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
} else {
echo "<script>alert('Data berhasil ditambah.');window.location='tambah.php';</script>";
}
}
Sementara itu fomulir untuk Create diatas:
<form class="contact100-form validate-form" method="POST" action="proses_tambah.php">
<div class="wrap-input100 validate-input" data-validate="Name is required">
<span class="label-input100">Nama Barang:</span>
<input class="input100" type="text" name="namabrg" placeholder="Masukkan nama barang" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex@abc.xyz">
<span class="label-input100">Jumlah:</span>
<input class="input100" type="number" name="jumlah" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Phone is required">
<span class="label-input100">Barang masuk:</span>
<input class="input100" type="date" name="brgmsk" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Message is required">
<span class="label-input100">Bagus:</span>
<input class="input100" name="bagus" type="number" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Message is required">
<span class="label-input100">Perbaikan:</span>
<input class="input100" name="perbaikan" type="number" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Message is required">
<span class="label-input100">Rusak:</span>
<input class="input100" name="rusak" type="number" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Message is required">
<span class="label-input100">Keterangan:</span>
<input class="input100" name="ket" type="text" placeholder="Masukkan keterangan!" />
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Message is required">
<span class="label-input100">Gambar Produk:</span>
<input class="input100" name="gambar" type="file" required />
<span class="focus-input100"></span>
</div>
<div class="container-contact100-form-btn">
<button class="contact100-form-btn">
<span>
Submit
<i class="fa fa-long-arrow-right m-l-7" aria-hidden="true"></i>
</span>
</button>
<a href="../invbrg.php" style="margin-left:15px;" class="contact100-form-btn">
<span>Back</span>
</a>
</div>
</form>
Bantuan akan saya aspresiasi, Terimakasih.
1 Jawaban:
Jawaban Terpilih
<div>coba form nya tambahin ini </div><pre>enctype="multipart/form-data"</pre><div><br>jadi nya seperti ini</div><pre><form class="contact100-form validate-form" method="POST" enctype="multipart/form-data" action="proses_tambah.php"></pre><div>semoga membantu</div>
Tanggapan
Terimakasih gan. Work.