Postingan lainnya
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result
mohon bantuan nya kesalahan nya di mana ya? Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in /Applications/MAMP/htdocs/latihan/latihan1/fungsi.php on line 12
isi file ubah.php
<?php
if ($_GET['username'] !== 'username') {
header("Location: index.php");
}
require 'fungsi.php';
$id = $_GET['id'];
$mhs = query("SELECT FROM mahasiswa WHERE id = $id");
die();
if (isset($_POST['tambah'])) {
if (ubah($_POST) > 0) {
$username = $_GET['username'];
echo "
<script>
alert ('Data Berhasil Di Tambahkan')
document.location.href = 'admin.php?username=$username'
</script>
";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Ubah Data Mahasiswa</title>
</head>
<body>
<h1>Tambah Data Mahasiswa</h1>
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $mhs['id']; ?>">
<table>
<?php for($baris = 0 ; $baris < 5 ; $baris++) : ?>
<tr>
<?php for($kolom = 0; $kolom < 2; $kolom++) :?>
<td>
<?php if($baris == 0 && $kolom == 0) : ?>
<label for="nrp">
NRP
</label>
<?php elseif($baris==0 && $kolom== 1) : ?>
<input type="text" name="nrp" value="<?php echo $mhs['nrp']; ?>">
<?php elseif($baris==1 && $kolom== 0) : ?>
<label for="nama">
Nama
</label>
<?php elseif($baris==1 && $kolom== 1) : ?>
<input type="text" name="nama" value="<?php echo $mhs['nama']; ?>">
<?php elseif($baris==2 && $kolom== 0) : ?>
<label for="email">
Email
</label>
<?php elseif($baris==2 && $kolom== 1) : ?>
<input type="text" name="email" value="<?php echo $mhs['email']; ?>">
<?php elseif($baris==3 && $kolom== 0) : ?>
<label for="jurusan">
Jurusan
</label>
<?php elseif($baris==3 && $kolom== 1) : ?>
<input type="text" name="jurusan" value="<?php echo $mhs['jurusan']; ?>">
<?php elseif($baris==4 && $kolom== 0) : ?>
<label for="gambar">
Gambar
</label>
<?php elseif($baris==4 && $kolom== 1) : ?>
<input type="file" name="gambar">
<?php endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
<button type="submit" name="tambah">Tambah Data</button>
<button type="submit" onclick="window.location.href='admin.php?username=<?php echo $_GET['username']; ?>'">
Kembali
</button>
</form>
</body>
</html>
isi file fungsi.php
<?php
$koneksi = mysqli_connect("localhost", "root", "root", "phpdasar");
function query($query) {
global $koneksi;
$result = mysqli_query($koneksi, $query);
$mahasiswa = [];
while ($data = mysqli_fetch_assoc($result)) {
$mahasiswa[] = $data;
}
return $mahasiswa;
}
function tambah($data) {
global $koneksi;
$nama = htmlspecialchars($data['nama']);
$nrp = htmlspecialchars($data['nrp']);
$email = htmlspecialchars($data['email']);
$jurusan = htmlspecialchars($data['jurusan']);
$gambar = upload();
if (!$gambar) {
return false;
}
$query = "INSERT INTO mahasiswa (nama,email,nrp,jurusan,gambar)
values (
'$nama',
'$email',
'$nrp',
'$jurusan',
'$gambar'
)
";
mysqli_query($koneksi, $query);
return mysqli_affected_rows($koneksi);
}
function upload() {
$namaGambar = $_FILES['gambar']['name'];
$ukuranGambar = $_FILES['gambar']['size'];
$tmpNAME = $_FILES['gambar']['tmp_name'];
$error = $_FILES['gambar']['error'];
// cek apakah ada gambar yang di upload
if ($error === 4) {
echo "
<script>
alert ('Silahkan Upload Gambar Terlebih Dahulu')
</script>
";
return false;
}
// cek apakah yang di upload user adalah file gambar
$ekstensiGambarValid = ['jpeg', 'jpg', 'img', 'png'];
$ekstensiGambar = explode('.', $namaGambar);
$ekstensiGambar = strtolower(end($ekstensiGambar));
if (in_array($ekstensiGambar, $ekstensiGambarValid) === false) {
echo "
<script>
alert ('File Yang Anda Upload Bukan Gambar !!!')
</script>
";
return false;
}
//cek apakah ukuran gambar melebihi kententuan
if ($ukuranGambar > 1000000) {
echo "
<script>
alert ('Ukuran Gambar Yang Anda Masukan Terlalu Besar')
</script>
";
return false;
}
$namaGambarBaru = uniqid();
$namaGambarBaru .= ".";
$namaGambarBaru .= $ekstensiGambar;
move_uploaded_file($tmpNAME, 'gambar/' . $namaGambarBaru);
return $namaGambarBaru;
}
function hapus($data) {
global $koneksi;
$query = "DELETE FROM mahasiswa WHERE id = $data ";
mysqli_query($koneksi, $query);
return mysqli_affected_rows($koneksi);
}
function ubah($data) {
$nama = $data['nama'];
$nrp = $data['nrp'];
$jurusan = $data['jurusan'];
$email = $data['email'];
}
?>
terimakasih sebelum nya
0
1 Jawaban:
Pertnayaan ini sudah sering ditanyakan, coba kamu <a href='https://sekolahkoding.com/forum?search=mysqli_fetch_assoc'>search disini</a>
0