CARA LOGIN MENGGUNAKAN 3 TABLE Di PHP

Mohon Bantuannya Suhu, Bagaimana Caranya agar bisa login menggunakan 3 table yang terhubung

contoh 1 : Berhasil Login Menggunakan 2 Table

if($_SESSION['admin'] || $_SESSION['SISWA'] || $_SESSION['GURU']){

$ambil = $koneksi->query("SELECT * FROM login, siswa WHERE login.username='$username' AND login.password='$password' AND login.kode_login='$kode_login' and siswa.status='aktif' and siswa.kode_login='$kode_login' ");

Contoh 2 : Gagal Login Menggukan 3 Table

if($_SESSION['admin'] || $_SESSION['SISWA'] || $_SESSION['GURU']){

$ambil = $koneksi->query("SELECT * FROM login, siswa, guru WHERE login.username='$username' AND login.password='$password' AND login.kode_login='$kode_login' and siswa.status='aktif' and siswa.kode_login='$kode_login' and guru.kode_login='$kode_login' ");

Kira Kira Salahnya Dimana Yah Suhu untuk contoh 2; terima kasih.

avatar bento94
@bento94

9 Kontribusi 0 Poin

Diperbarui 1 bulan yang lalu

4 Jawaban:

if($_SESSION['admin'] || $_SESSION['SISWA'] || $_SESSION['GURU']){
    $ambil = $koneksi->query("SELECT * FROM login 
                              INNER JOIN siswa ON login.kode_login = siswa.kode_login AND siswa.status = 'aktif'
                              INNER JOIN guru ON login.kode_login = guru.kode_login
                              WHERE login.username = '$username' 
                              AND login.password = '$password' 
                              AND login.kode_login = '$kode_login'");
}

Saya menggunakan INNER JOIN untuk menggabungkan tabel siswa dan guru dengan login.

Kondisi WHERE digunakan untuk memeriksa username, password, dan kode_login.

Pastikan bahwa variabel $username, $password, dan $kode_login telah disanitasi sebelum digunakan dalam query untuk mencegah SQL Injection.

avatar adamajalah27
@adamajalah27

119 Kontribusi 39 Poin

Dipost 2 bulan yang lalu

Tetap Nggak Bisa suhu

<?php

if (isset($_POST['login'])) {

  $kode_login=$_POST['kode_login'];
  $username=$_POST['username'];
  $password=$_POST['password'];

$ambil = $koneksi->query("SELECT * FROM login 
INNER JOIN siswa ON login.kode_login = siswa.kode_login AND siswa.status = 'aktif'
INNER JOIN guru ON login.kode_login = guru.kode_login
WHERE login.username = '$username' AND login.password = '$password' AND login.kode_login = '$kode_login'");

  $data =$ambil->fetch_assoc();
  
  $ketemu = $ambil->num_rows;
  if($ketemu >=1){

    session_start();
    
    if($data['level'] == "admin"){
        $_SESSION['admin'] = $data[kode_login];
        ?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "BERHASIL LOGIN",
                      text: "<?php echo $data['nama']; ?>",
                      timer: 2000,
                      type: "success"
                     
                  }, function() {
                      window.location = "admin/index.php";
                  });
              }, 300);
          </script>
            <?php

    }else if($data['level'] == "SISWA"){
        $_SESSION['SISWA'] = $data[kode_login];
?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "BERHASIL LOGIN",
                      text: "<?php echo $data['nama']; ?>",
                      timer: 2000,
                      type: "success"
                  }, function() {
                      window.location = "admin/index.php";
                  });
              }, 300);
          </script>
            <?php

    }else if($data['level'] == "GURU"){
        $_SESSION['GURU'] = $data[kode_login];
?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "BERHASIL LOGIN",
                      text: "<?php echo $data['nama']; ?>",
                      timer: 2000,
                      type: "success"
                  }, function() {
                      window.location = "admin/index.php";
                  });
              }, 300);
          </script>
            <?php
            }
          

}else{
          ?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "LOGIN GAGAL !",
                      text: "ATAU AKUN ANDA DINONAKTIFKAN",
                      timer: 2000,
                      type: "error"
                  }, function() {
                     window.location = "login.php";
                  });
              }, 300);
          </script>
            <?php
}

}
}
?>

tetap nggak bisa perintah seperti itu, kira kira kenapa yah keterangan : login gagal (alert)

avatar bento94
@bento94

9 Kontribusi 0 Poin

Dipost 2 bulan yang lalu

  1. Masalah pertama
$_SESSION['admin'] = $data[kode_login];
$_SESSION['SISWA'] = $data[kode_login];
$_SESSION['GURU'] = $data[kode_login];

apakah ini benar penulisannya solusinya

 $data['kode_login'];
  1. Masalah kedua data pada field ada atau tidak karena terdapat huruf besar dan huruf kecil tidak konsistem Solusi
$data['level']
  1. Masalah Ketiga di select mysql tentukan apa yang yang mau dipilih jangan sampai sama nama fieldnya
"SELECT * FROM login 
INNER JOIN siswa ON login.kode_login = siswa.kode_login AND 
siswa.status = 'aktif'
INNER JOIN guru ON login.kode_login = guru.kode_login
WHERE login.username = '$username' AND login.password = '$password' 
AND login.kode_login = '$kode_login'"

solusinya

"SELECT login.kode_login, login.username, login.password, login.level 
FROM login 
INNER JOIN siswa ON login.kode_login = siswa.kode_login AND 
siswa.status = 'aktif'
INNER JOIN guru ON login.kode_login = guru.kode_login
WHERE login.username = '$username' AND login.password = '$password' 
AND login.kode_login = '$kode_login'"
avatar lionkip
@lionkip

64 Kontribusi 49 Poin

Dipost 2 bulan yang lalu

tetap nggak bisa suhu,kira kira apa nya yah contoh script

<?php
  error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
  ob_start();
  session_start();
  include "koneksi/koneksi.php";

  if($_SESSION['ADMIN'] || $_SESSION['SISWA'] || $_SESSION['GURU']){
  
    }else{

?>
<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>SMA N 4</title>
  <link rel="icon" type="image/png" href="images/logo4.png">
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="admin/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="admin/bower_components/font-awesome/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="admin/bower_components/Ionicons/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="admin/dist/css/AdminLTE.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="admin/plugins/iCheck/square/blue.css">

  <link rel="stylesheet" type="text/css" href="sw/dist/sweetalert.css">
  <script type="text/javascript" src="sw/dist/sweetalert.min.js"></script>

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head>
<style type="text/css">
  #bodi{
    background-image: url(img/logo_SS.png);
   height: 40%;
   width: 40%;
   display: flex;

    background-repeat: no-repeat;
  }
  body{
            background-image: url(images/sma4.jpg);
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
            background-attachment: fixed;
            height: 100%;
        }
#im{margin-left: 700px; margin-top: 70px; height: 150px; width: 170px;}
</style>

<a href="" data-toggle="modal" data-target="#modal-login">
<img id="im" src="images/lo.png">
</a>



<!-- /.login-box -->





<div class="modal fade" id="modal-login">
          <div class="modal-dialog modal-sm">
            <div class="modal-content">
              <div class="modal-header" style="background-color: #1ec38c;">
            
                 <center>
                     <img src="images/logo4.png">
                 </center>
              </div>
              <div class="modal-body">
                    <form id="sign_in" method="POST">
                      <div class="input-group">
                        <span class="input-group-addon">
                            <i class="fa fa-key"></i>
                        </span>
                        <div class="form-line">
                            <input type="text" class="form-control" name="kode_login" placeholder="Kode Login " required autofocus>
                        </div>
                    </div>
                <div class="input-group" style="margin-top: 5px;">
                        <span class="input-group-addon">
                            <i class="fa fa-user"></i>
                        </span>
                        <div class="form-line">
                            <input type="text" class="form-control" name="username" placeholder="Username" required>
                        </div>
                    </div>
                    <div class="input-group" style="margin-top: 5px;">
                        <span class="input-group-addon">
                            <i class="fa fa-lock"></i>
                        </span>
                        <div class="form-line">
                            <input type="password" class="form-control" name="password" placeholder="Password" required>
                            
                        </div>
                    </div><br><br>



                    <input style=" font-weight: bold; height: 50px; font-size: 15px; background-color: #1ec38c;;color: white;"  type="submit" name="login" class="btn btn-block" value="M A S U K"/>
                </form>
<?php

if (isset($_POST['login'])) {

  $kode_login=mysqli_real_escape_string($koneksi,$_POST['kode_login']);
  $username=mysqli_real_escape_string($koneksi,$_POST['username']);
  $password=mysqli_real_escape_string($koneksi,$_POST['password']);


$ambil = $koneksi->query("SELECT login.kode_login, login.username, login.password FROM login INNER JOIN siswa ON login.kode_login = siswa.kode_login AND siswa.status = 'aktif' INNER JOIN guru ON login.kode_login = guru.kode_login WHERE login.username = '$username' AND login.password = '$password' AND login.kode_login = '$kode_login'");

  $data =$ambil->fetch_assoc();
  
  $ketemu = $ambil->num_rows;
  if($ketemu >=1){

    session_start();
    
    if($data['level'] == "ADMIN"){
        $_SESSION['ADMIN'] = $data[kode_login];
        ?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "BERHASIL LOGIN",
                      text: "",
                      timer: 2000,
                      type: "success"
                     
                  }, function() {
                      window.location = "admin/index.php";
                  });
              }, 300);
          </script>
            <?php

    }else if($data['level'] == "SISWA"){
        $_SESSION['SISWA'] = $data[kode_login];
?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "BERHASIL LOGIN",
                      text: "<?php echo $data['nama']; ?>",
                      timer: 2000,
                      type: "success"
                  }, function() {
                      window.location = "admin/index.php";
                  });
              }, 300);
          </script>
            <?php

    }else if($data['level'] == "GURU"){
        $_SESSION['GURU'] = $data[kode_login];
?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "BERHASIL LOGIN",
                      text: "<?php echo $data['nama_G']; ?>",
                      timer: 2000,
                      type: "success"
                  }, function() {
                      window.location = "admin/index.php";
                  });
              }, 300);
          </script>
            <?php
          }
          
}else{
          ?>
                <script>
              setTimeout(function() {
                  swal({
                      title: "LOGIN GAGAL !",
                      text: "ATAU AKUN ANDA DINONAKTIFKAN",
                      timer: 2000,
                      type: "error"
                  }, function() {
                     window.location = "login.php";
                  });
              }, 300);
          </script>
            <?php
}

}
}
?>
                

              </div> 
            <!-- /.modal-content -->
          </div>
          <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->

<!-- jQuery 3 -->
<script src="admin/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="admin/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="admin/plugins/iCheck/icheck.min.js"></script>




<!-- SlimScroll -->
<script src="admin/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->

<!-- AdminLTE App -->
<script src="admin/dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="admin/dist/js/demo.js"></script>

<script>
  $(function () {
    $('input').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' // optional
    });
  });
</script>
</body>
</html>
avatar bento94
@bento94

9 Kontribusi 0 Poin

Dipost 1 bulan yang lalu

Login untuk ikut Jawaban