PHP Pemula - Fatal error: Uncaught TypeError: mysqli_query():

Mohon bantuannya, errornya tampil seperti ini :
Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given in D:\xampp\htdocs\phpdasar\pertemuan10\functions.php:6 Stack trace: #0 D:\xampp\htdocs\phpdasar\pertemuan10\functions.php(6): mysqli_query(NULL, 'SELECT * FROM m...') #1 D:\xampp\htdocs\phpdasar\pertemuan10\index.php(3): query('SELECT * FROM m...') #2 {main} thrown in D:\xampp\htdocs\phpdasar\pertemuan10\functions.php on line 6

functions.php
<?php 
// koneksi ke database

function query ($query){
	global $conn;
	$result = mysqli_query ($conn, $query);
	$rows = [];
	while ($row = mysqli_fetch_assoc($result)) {
		$rows[] = $row;
	}

	return $rows;
}


function tambah ($data){
	global $conn;
	// ambil data dari tiap elemen dalam form
	$nim = $data ["nim"];
	$nama= $data["nama"];
	$email= $data["email"];
	$jurusan= $data["jurusan"];
	$gambar= $data ["gambar"];
	
	$query = "INSERT INTO mahasiswa
				VALUES
				('', '$nim','$nama','$email','$jurusan', '$gambar');

				";

		mysqli_query ($conn, $query);

		return mysqli_affected_rows ($conn);

}
?>











index.php
<?php 
require 'functions.php';
$mahasiswa = query ("SELECT * FROM mahasiswa");

?>


<!DOCTYPE html>
<html>
<head>
	<title>Halaman Admin</title>
</head>
<body>

<h1>Daftar Mahasiswa</h1>

<a href="tambah.php">Tambah Data Mahasiswa </a>
<br><br>

<table border="1" cellpadding="10" cellspacing="0">
	
	<tr>
		<th>No.</th>
		<th>Aksi</th>
		<th>Gambar</th>
		<th>NIM</th>
		<th>Nama</th>
		<th>Email</th>
		<th>Jurusan</th>
	</tr>
	<?php $i = 1; ?>
	<?php foreach ($mahasiswa as $row) :
	 ?>
	<tr>
		<td><?php echo $i; ?></td>
		<td>
			<a href="">ubah</a> |
			<a href="">hapus</a>
		</td>
		<td><img src="img/<?php echo $row ["gambar"];?>" width="50"></td>
		<td><?php echo $row ["nim"]; ?></td>
		<td><?php echo $row ["nama"]; ?></td>
		<td><?php echo $row ["email"]; ?></td>
		<td><?php echo $row ["jurusan"]; ?></td>
	</tr>
<?php $i++; ?>
<?php endforeach; ?>
</table>


</body>
</html>

tambah.php
<?php
 // Koneksi ke DMS

require 'functions.php';

 $conn = mysqli_connect("localhost", "root", "", "phpdasar");

// cek apakah tombol submit sudah ditekan atau belum

if (isset($_POST["submit"])) {

	// cek apakah berhasil ditambahkan/tidak

	if (tambah($_POST) > 0) {
		echo "data berhasil ditambahkan";
	} else {
		echo "data gagal ditambahkan";
	}

}


?>

<!DOCTYPE html>
<html>
<head>
	<title>Tambah Data Mahasiswa</title>
</head>
<body>

	<h1>Tambah data mahasiswa</h1>

	<form action="" method="post">
		<ul>

			<label for="nim">NIM :</label>
			<li>
				<input type="text" name="nim" id="nim">
			</li>

			<label for="nama">Nama :</label>
			<li>
				<input type="text" name="nama" id="nama">
			</li>


			<label for="email">Email :</label>
			<li>
				<input type="text" name="email" id="email">
			</li>

			<label for="jurusan">Jurusan :</label>
			<li>
				<input type="text" name="jurusan" id="jurusan">
			</li>

			<label for="gambar">Gambar :</label>
			<li>
				<input type="text" name="gambar" id="gambar">
			</li>

			<li>
				<button type="submit" name="submit">Tambah Data !</button>
			</li>
		</ul>
		

	</form>

</body>
</html>
avatar xxxxbin

@xxxxbin

3 Kontribusi 1 Poin


Jawaban Terpilih

Di index.php, kamu belum buat variabel $conn seperti yang di ada di tambah.php:
$conn = mysqli_connect("localhost", "root", "", "phpdasar");
avatar quikzens

@quikzens

1 Kontribusi 3 Poin

Jawaban

Di index.php, kamu belum buat variabel $conn seperti yang di ada di tambah.php:
$conn = mysqli_connect("localhost", "root", "", "phpdasar");
avatar quikzens

@quikzens

1 Kontribusi 3 Poin


Login untuk gabung berdiskusi