Warning: Undefined variable $kode_barang in C:\xampp\htdocs\inventory\supplier.php on line 186

Kesalahan Detailnya :

1.) Warning: Undefined variable $kode_barang in C:\xampp\htdocs\inventory\supplier.php on line 186

2.) Notice: Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\inventory\supplier.php on line 187

3.) Fatal error: Uncaught TypeError: mysqli_num_rows(): Argument #1 ($result) must be of type mysqli_result, bool given in C:\xampp\htdocs\inventory\supplier.php:187 Stack trace: #0 C:\xampp\htdocs\inventory\supplier.php(187): mysqli_num_rows(true) #1 {main} thrown in C:\xampp\htdocs\inventory\supplier.php on line 187

Code Line 186 :

$cek = mysqli_query($connect, "SELECT * FROM barang WHERE kode_barang = '$kode_barang=null'");

Code Line 187 :

if (mysqli_num_rows($cek > 0))

Full Code :

<?php
if (isset($_POST['submit'])) {
	$kode = $_POST['kode'];
	$kode_supplier = $kode.$newKode;
	$nama = $_POST['nama'];
	$kota = $_POST['kota'];
	$telp = $_POST['telp'];
	$alamat = $_POST['alamat'];

	$cek = mysqli_query($connect, "SELECT * FROM barang WHERE kode_barang = '$kode_barang=null'");
	if (mysqli_num_rows($cek > 0)) {
		$_SESSION['danger'] = "Maaf, Kode barang tersebut sudah ada, mohon coba lagi";
	}
	else {
		$query = mysqli_query($connect, "
		INSERT INTO supplier(kode_supplier, nama_supplier, alamat_supplier, telp_supplier, kota_supplier)
		VALUES ('$kode_supplier', '$nama', '$alamat', '$telp', '$kota') ");
		if ($query) {
			$_SESSION['success'] = "Data berhasil dimasukkan";
			echo "<script>location.replace('supplier.php');</script>";
		}
		else {
			$_SESSION['danger'] = "Error saat memasukkan data";
		}
	}
}

?>
avatar Y04Y
@Y04Y

5 Kontribusi 2 Poin

Diperbarui 3 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

<div>Coba baris ini :</div><pre> $cek = mysqli_query($connect, "SELECT * FROM barang WHERE kode_barang = '$kode_barang=null'");

if (mysqli_num_rows($cek &amp;gt; 0)) {
	$_SESSION[&#039;danger&#039;] = &quot;Maaf, Kode barang tersebut sudah ada, mohon coba lagi&quot;;
}&lt;/pre&gt;&lt;div&gt;&lt;br&gt;diubah menjadi :&lt;/div&gt;&lt;pre&gt;        $cek = mysqli_query($connect, &quot;SELECT * FROM barang WHERE kode_barang = &#039;$kode&#039;&quot;); //Karena diatas nama variabelnya $kode

if (mysqli_num_rows($cek) &amp;gt; 0) { //tanda &amp;gt; 0 harusnya diluar tanda tutup kurung pertama 
	$_SESSION[&#039;danger&#039;] = &quot;Maaf, Kode barang tersebut sudah ada, mohon coba lagi&quot;;
}&lt;/pre&gt;&lt;div&gt;&lt;br&gt;Semoga berhasil.&lt;/div&gt;
avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban