Menampilkan Data dan Gambar Dari Database menggunakan Session

Permisi agan-agan, mohon maaf sebelumnya saya baru belajar koding. jadi begini gan/sist kemaren saya buat program dimana salah satu fungsinya adalah harus bisa menampilkan data diri pengguna yang sedang login dan fotonya. kemudian setelah saya coba2 tetapi yang nampil seluruh data pengguna yg ada di database (seharusnya data pengguna yg sedang login saja). mohon pencerahannya gan/sist..

terima kasih

 <?php
//----------------inisialisasi get p
$username=$_SESSION['username'];
$query = "select * from data_penghuni order by date DESC";
$sql_b = mysqli_query($connect, $query);
$jum_data = mysqli_num_rows($sql_b);

//------------------menampilkan form isi data

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Sistem Informasi Asrama Mahasiswa Politeknik Negeri Batam</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet">
<link href="css/animate.min.css" rel="stylesheet">
<link href="css/prettyPhoto.css" rel="stylesheet">
<link href="css/main.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/responsive.css" rel="stylesheet">
<link href="css/swiper.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
<link rel="shortcut icon" href="images/favicon1.png">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="images/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="images/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="images/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="images/ico/apple-touch-icon-57-precomposed.png">

<script src="js/jquery.js"></script>
<script src="js/basic-jquery-slider.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.prettyPhoto.js"></script>
<script src="js/jquery.isotope.min.js"></script>
<script src="js/main.js"></script>
<script src="js/wow.min.js"></script>
</head>
<body class="homepage">
<?php
while($data = mysqli_fetch_array($sql_b)){
	$id_penghuni = $data['id_penghuni'];

?>
<div>
<h2 style="margin-top:10px;color:#666666;text-align:center;margin:20px;">Data Diri - <?php echo $data['nama_lengkap']; ?></h2>
</div>
<div class="container">
	<div class="row">
		<div class="col-group">
			<label class="col-sm-3 control-label" ><font size="3.5"><b>ID Penghuni </font><label style="color: red;"></label></b></label>
			<div class="col-sm-9"><input class="form-control" type="text" name="id_penghuni" value="<?php echo $data['id_penghuni'] ?>" disabled /></div>
		</div>
		<div class="col-xs-offset-0 col-md-9">
			<table class="table table-bordered"  style="height:100px;width:100%;">
				<tr align="center">
					<font size="4"><b>A. Identitas Pribadi</font>
				</tr><br><br>
				<tr>
					<th>Nama Lengkap</th><td><?php echo $data['nama_lengkap']; ?></td>
				</tr>
				<tr>
					<th>Jenis Kelamin</th><td><?php echo $data['jenis_kelamin']; ?></td>
				</tr>
				<tr>
					<th>NIM</th><td><?php echo $data['NIM']; ?></td>
				</tr>
				<tr>
					<th>TTL</th><td><?php echo $data['TTL']; ?></td>
				</tr>
				<tr>
					<th>Alamat Asal</th><td><?php echo $data['alamat_asal']; ?></td>
				</tr>
				<tr>
					<th>Kode Pos</th><td><?php echo $data['kodepos_asal']; ?></td>
				</tr>
				<tr>
					<th>Alamat Batam</th><td><?php echo $data['alamat_batam']; ?></td>
				</tr>
				<tr>
					<th>Kode Pos</th><td><?php echo $data['kodepos_batam']; ?></td>
				</tr>
				<tr>
					<th>No Telp</th><td><?php echo $data['notelp']; ?></td>
				</tr>
			</table>
		</div>

		<div class="col-sm-9 col-md-3"><br><br><br>
			<div class="thumbnail" style="height:310px;width:260px">
				<?php
					$query = "select * from gambar_dataPenghuni where id_penghuni = '$idpenghuni'";
					$sql = mysqli_query($connect, $query);
					if(mysqli_num_rows($sql)>0){
						$photo = "";
				?>
				<div class="swiper-container gallery-top">
					<div class="swiper-wrapper">
						<?php while($x = mysqli_fetch_array($sql)){ ?>
						<div class="swiper-slide" style="background-image:url(<?php echo $x['url'] ?>)"></div>
						<?php }; ?>
					</div>
				<!-- Add Arrows-->
					<div class="swiper-button-next swiper-button-white"></div>
					<div class="swiper-button-prev swiper-button-white"></div>
				</div>
				<div class="swiper-container gallery-thumbs">
					<div class="swiper-wrapper">
						<?php
						$thumb = mysqli_query($connect, $query);
						while($xa = mysqli_fetch_array($thumb)){ ?>
						<div class="swiper-slide" style="background-image:url(<?php echo $xa['url'] ?>)"></div>
						<?php }; ?>
					</div>
				</div>
				<?php
					} else {
						$photo = "<h2 align='center'><em>Foto tidak tersedia</em></h2>";
					}
				echo $photo;
				?>
				<script src="js/swiper.min.js"></script>
				<script>
				    var galleryTop = new Swiper('.gallery-top', {
				        nextButton: '.swiper-button-next',
				        prevButton: '.swiper-button-prev',
				        spaceBetween: 10,
				    });
				    var galleryThumbs = new Swiper('.gallery-thumbs', {
				        spaceBetween: 10,
				        centeredSlides: true,
				        slidesPerView: 'auto',
				        touchRatio: 0.2,
				        slideToClickedSlide: true
				    });
				    galleryTop.params.control = galleryThumbs;
				    galleryThumbs.params.control = galleryTop;

				</script>
			</div>
		</div>
		<div class="col-xs-offset-0 col-md-12">
			<table class="table table-bordered"  style="height:100px;width:100%;">
				<tr align="center">
					<font size="4"><b>B. Identitas Wali/Saudara</font>
				</tr><br><br>
				<tr>
					<th width="18.3%">Nama Lengkap</th><td><?php echo $data['nama_wali']; ?></td>
				</tr>
				<tr>
					<th>Alamat</th><td><?php echo $data['alamat_wali']; ?></td>
				</tr>
				<tr>
					<th>kode Pos</th><td><?php echo $data['kodepos_wali']; ?></td>
				</tr>
				<tr>
					<th>No Telp</th><td><?php echo $data['notelp_wali']; ?></td>
				</tr>
				<tr>
					<th>Pekerjaan</th><td><?php echo $data['pekerjaan_wali']; ?></td>
				</tr>
			</table>
		</div>
		<div class="col-xs-offset-0 col-md-12">
			<table class="table table-bordered"  style="height:100px;width:100%;">
				<tr align="center">
					<font size="4"><b>C. Identitas Orang Tua</font>
				</tr><br><br>
				<tr>
					<th width="18.3%">Nama Ayah</th><td><?php echo $data['nama_ayah']; ?></td>
				</tr>
				<tr>
					<th>Nama Ibu</th><td><?php echo $data['nama_ibu']; ?></td>
				</tr>
				<tr>
					<th>Alamat</th><td><?php echo $data['alamat_ortu']; ?></td>
				</tr>
				<tr>
					<th>kode Pos</th><td><?php echo $data['kodepos_ortu']; ?></td>
				</tr>
				<tr>
					<th>No Telp</th><td><?php echo $data['notelp_ortu']; ?></td>
				</tr>
				<tr>
					<th>Pekerjaan</th><td><?php echo $data['pekerjaan_ortu']; ?></td>
				</tr>
				<tr>
					<th>Pedapatan</th><td><?php echo $data['pendapatan_ortu']; ?></td>
				</tr>
			</table>
		</div>
	</div>
</div>
</div>
</body>
</html>
<div class="container" align="center" style="margin-top:10px; margin-bottom:50px;">


</div>
<?php }

?>

di gambarnya terlihat lebih dari 1 data yang masuk (seharusnya 1 data saja n fotonya masih error)

avatar Ripon
@Ripon

9 Kontribusi 0 Poin

Diperbarui 6 tahun yang lalu

9 Jawaban:

<pre> $username=$_SESSION['username']; $query = "select * from data_penghuni WHERE username = '$username' order by date DESC"; </pre>

avatar Nandar
@Nandar

648 Kontribusi 204 Poin

Dipost 6 tahun yang lalu

error jadinya mas @Nandar <img src='https://i.imgur.com/7Aikaf1.png'>

avatar Ripon
@Ripon

9 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

di echo $_SESSION['username'] nya dapet ga? session_start nya juga belum di deklarasi.

avatar Nandar
@Nandar

648 Kontribusi 204 Poin

Dipost 6 tahun yang lalu

ngeceknya bagaimana ya mas. maklum saya baru belajar dengan session

SESSION START-nya sudah ada di index mas

<pre> &lt;?php ob_start(); session_start(); include "../service/connect.php"; ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Sistem Informasi Asrama Mahasiswa Politeknik Negeri Batam&lt;/title&gt; &lt;!--&lt;link rel="stylesheet" href="style.css"&gt;--&gt; &lt;link rel="shortcut icon" href="../images/logo3.png"&gt; &lt;link href="../css/bootstrap.min.css" rel="stylesheet"&gt; &lt;link href="../css/bootstrap.css" rel="stylesheet" /&gt; &lt;link href="../css/style2.css" rel="stylesheet" /&gt; &lt;link href="../css/font-awesome.min.css" rel="stylesheet"&gt; &lt;link href="../css/animate.min.css" rel="stylesheet"&gt; &lt;link href="../css/prettyPhoto.css" rel="stylesheet"&gt; &lt;link href="../css/main.css" rel="stylesheet"&gt; &lt;link href="../css/style.css" rel="stylesheet"&gt; &lt;link href="../css/responsive.css" rel="stylesheet"&gt; &lt;link href="../css/custombox.min.css" rel="stylesheet"&gt;

&lt;script src="../js/custombox-min.js"&gt;&lt;/script&gt; &lt;script src="../js/jquery.js"&gt;&lt;/script&gt; &lt;script src="../js/overlay.js"&gt;&lt;/script&gt; &lt;script src="ckeditor/ckeditor.js"&gt;&lt;/script&gt; &lt;script src="ckeditor/styles.js"&gt;&lt;/script&gt;

&lt;!-- &lt;script src="//cdn.ckeditor.com/4.5.7/full/ckeditor.js"&gt;&lt;/script&gt; --&gt; &lt;/head&gt;

&lt;body&gt; &lt;?php include "templates/header.php"; //$level=$_SESSION['level']; if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['nama']) && $level=='Penghuni'){ if(isset($_GET['page'])){ $page = $_GET['page']; if(!empty($page)){ if ($page == 'data-diri'){//fix include "templates/data-diri.php";//fix </pre>

kalau di data-diri.php dimasukin juga jadinya begini ntar <img src='https://i.imgur.com/9thme0u.png'>

avatar Ripon
@Ripon

9 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

itu kolom username ada ga?

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

kolom dimananya ya mas?

avatar Ripon
@Ripon

9 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

agan nyoba saran dari agan @Nandar? kan itu ada "WHERE username = '$username'", lah kolom username ditable data_pengguni itu ada atau tidak?

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

ohh... paham2 sebentar ya mas tidak ada kolom usernamenya

#edit lalu setelah itu bagaimana mas. kan mestinya ngambil data usernamenya dari user login kan ya?? gimana tu??

avatar Ripon
@Ripon

9 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

ini saya coba2 tadi kan data di databasenya seperti ini <img src='https://i.imgur.com/98I5WMK.png'>

lalu scriptnya saya ganti seperti ini

<pre> $idakun=$_SESSION['id_akun']; $query = "select * from data_penghuni WHERE id_akun = '$idakun' order by date DESC"; $sql_b = mysqli_query($connect, $query); </pre>

tapi kok malah ngga muncul apa2 ya mas??

avatar Ripon
@Ripon

9 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban