Dimana letak kesalahan berikut sehingga data gagal diupdate ke data base?


<?php

$konek = mysqli_connect("localhost", "root", "", "apk_spp" );

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

function ubah_kelas($dataK) {
	global $konek;

	$nama = $dataK["Nama"];
	$guru = $dataK["nama_guru"];

	$query = "UPDATE tb_siswa SET
				id_guru = '$guru'
			WHERE nama_kelas = '$nama'
			";
	mysqli_query($konek, $query);

	return mysqli_affected_rows($konek);

}
?>
<?php
session_start();

if( !isset($_SESSION["login"]) ){
	header("Location: login.php");
	exit;
}
// koneksi ke DBMS
require 'fungsi.php';

//ambil data di url
$nama =$_GET["nama"];

//query data kelas berdasarkan id
$kls = query ("SELECT * FROM tb_kelas WHERE nama_kelas = '$nama' ")[0];

//cek tombol submit
if ( isset ($_POST["submit"]) ) {

	//cek data yang diubah
	if( ubah_kelas ($_POST) > 0){
		echo "
			<script>
			alert('Data Berhasil Diubah');
			document.location.href = 'kelas.php';
			</script>
		";
	} else {
	 	echo "
			<script>
			alert('Data Gagal Diubah');
			document.location.href = 'kelas.php';
			</script>
		";
	}

}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">

	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

	<title>ubah data</title>

	<link href="gaya/css/bootstrap.min.css" rel="stylesheet">
    <link href="gaya/css/style.css" rel="stylesheet">
    <link rel="stylesheet" href="stail.css">

</head>
<?php include "header.php"; ?>
<body>
<div class="col-md-4 col-md-offset-4 form-login">
	<div class="outter-form-login">

		<form action="" method="post">
		<div class="form-group">

			<h1 class="text-center title-login page-header">Ubah Data Kelas</h1>
			<p><a class="btn btn-primary" href="kelas.php">Kembali</a></p>

			<div class="form-group">
			<input type="text" class="form-control" name="Nama" value="<?= $kls["nama_kelas"]; ?>" readonly>
			</div>

			<div class="form-group">
			<input type="text" class="form-control" name="Kompetensi" value="<?= $kls["kompetensi_keahlian"]; ?>" readonly>
			</div>

			<div class="form-group">
			<select name="nama_guru" class="form-control">
				<?php $gwk = query("SELECT * FROM tb_guru ORDER BY nama_guru ASC"); ?>
				<?php foreach ( $gwk as $gu ) : ?>
					<?php if ($gu["id_guru"] == $kls["id_guru"]) :?>
						<option value="<?= $gu["id_guru"]; ?>" selected> <?= $gu["nama_guru"]; ?> </option>
					<?php else : ?>
						<option value="<?= $gu["id_guru"]; ?>" > <?= $gu["nama_guru"]; ?> </option>
					<?php endif; ?>
				<?php endforeach; ?>
			</select>
			</div>

			<button class="btn btn-success" type="submit" name="submit">Ubah</button>
		</div>
		</form>
	</div>
</div>
<?php include "footer.php"; ?>
</body>
</html>

avatar kurb
@kurb

1 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

Tanggapan

tidak ada pesan error?

1 Jawaban:

untuk bagian query jika ada variable coba pake double quote jangan single quote karena double quote pada dasarnya akan mengolah variable sedangkan single quote akan menganggap yang diisi didalamnya adalah string biasa... kemungkinan besar jika tak bisa diupdate kesalahannya pada penggunaan single quote dan double quote

single quote = '...' double quote = "..."

avatar pemudakoding
@pemudakoding

342 Kontribusi 120 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban