alexa-tracking

gagal koneksi ke mysql

Main Content

1024
1024
KASKUS
51
244
https://www.kaskus.co.id/thread/5b1e9f2c96bde675538b4568/gagal-koneksi-ke-mysql
gagal koneksi ke mysql
misi minta bantuan nya gan , saya legi ngerjain project web untuk uas . jadi saya masih bikin form login udah ada database nya tapi gamau konek user ama pass nya ke mysql. mohon pencerahanya mastah.
Code:
<?php
session_start();
if($_SESSION){
header("Location: user.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<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>Login System</title>
 
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color:#eee;
}
.row {
margin:100px auto;
width:300px;
text-align:center;
}
.login {
background-color:#fff;
padding:20px;
margin-top:20px;
}
</style>
<!--[if lt IE 9]>
[removed][removed]
[removed][removed]
<![endif]-->
</head>
<body>

<div class="container">
<div class="row">
<h2>Masuk Gan!</h2>
<div class="login">

<?php
if(isset($_POST['login'])){
include("koneksi.php");

$username = $_POST['username'];
$password = md5($_POST['password']);
$level = $_POST['level'];

$query = mysqli_query($koneksi, "SELECT * FROM user WHERE username='$username' AND password='$password'");
if(mysqli_num_rows($query) == 0){
echo '<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
}else{
$row = mysqli_fetch_assoc($query);

if($row['level'] == 1 && $level == 1){
$_SESSION['username']=$username;
$_SESSION['level']='admin';
header("Location: user.php");
}else if($row['level'] == 2 && $level == 2){
$_SESSION['username']=$username;
$_SESSION['level']='user';
header("Location: .php");
}else{
echo '<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
}
}
}
?>

<form role="form" action="" method="post">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="Username" required autofocus />
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Password" required autofocus />
</div>
<div class="form-group">
<select name="level" class="form-control" required>
<option value="">Pilih Level User</option>
<option value="1">Administrator</option>
<option value="2">User</option>
<!-- <option value="3">Mahasiswa</option> -->
</select>
</div>
<div class="form-group">
<input type="submit" name="login" class="btn btn-primary btn-block" value="Capcuss" />
</div>
</form>
</div>
Copyright © 2K18 @AGUS,MI-1C
</div>
</div>
 
[removed][removed]
[removed][removed]
</body>
</html>[img]https://s.kaskus.id/images/2018/06/11/9269817_20180611111024.png[/img]

Quote:


isi file "koneksi.php" apaan?

EDIT: kalau diliat dari hasilnya, kayaknya ada typo di field databasenya
ada dua "Upss...!!! Login gagal." ente bakal sulit bedakan errornya dari baris mana.

asumsi: isi file koneksi.php udah bener
bagian atas
PHP Code:
<?php
session_start
();
if(
$_SESSION){
  switch(
$_SESSION['level']) {
    case 
'admin'header("location: admin.php"); exit;
    case 
'user'header("location: user.php"); exit;
  }
}
?>

bagian proses
PHP Code:
<?php
if(isset($_POST['login'])){
  include(
"koneksi.php");
  
$username $_POST['username'];
  
$password md5($_POST['password']);
  
$level  $_POST['level'];
  
$query mysqli_query($koneksi"SELECT * FROM user WHERE username='$username' AND password='$password'");
  if(
mysqli_num_rows($query) == 0){
    echo 
'<div class="alert alert-danger">Upss...!!! Error Username/Password</div>';
  }else{
    
$row mysqli_fetch_assoc($query);
    if(
$row['level'] == && $level == 1){
      
$_SESSION['username']=$username;
      
$_SESSION['level']='admin';
      
header("Location: admin.php");
    }else if(
$row['level'] == && $level == 2){
      
$_SESSION['username']=$username;
      
$_SESSION['level']='user';
      
header("Location: user.php");
    }else{
      echo 
'<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
    }
  }
}
?>
emoticon-Sundul
Quote:

sehat? gak salah ambil obat?