Gusti Aldi Gusti Aldi - 6 months ago 18
SQL Question

login page with php and mysql

i tried create login page, but it says "Notice: Undefined variable: error in .....\login.php on line 68"

after i re-check me already declare that $error variable before, what kind solution to solve this problem ?

line 68 is :

<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php if($error){echo $error;} ?></div>


and here's the code

<?php
include("connection.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);

$sql = "SELECT id FROM tb_user WHERE username = '$myusername' and password = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
session_register("myusername");
$_SESSION['login_user'] = $myusername;

header("location: welcome.php");
}else {
$error = "user atau password anda salah";
}
}
?>
<html>

<head>
<title>Login Page</title>

<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

label {
font-weight:bold;
width:100px;
font-size:14px;
}

.box {
border:#666666 solid 1px;
}
</style>






<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

<div style = "margin:30px">

<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>

<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php if($error){echo $error;} ?></div>

</div>

</div>

</div>




Answer
// try out this 
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php if(isset($error)){echo $error;} ?></div>