I'm having trouble creating a login page. Set up a MySql database and table. The table has 3 fields; id, username & password. Every time I try to log in, I receive the message that the login failed. After hours of research, I still cannot pinpoint where this goes wrong.
Here's the code for the "login.htm" page:
<html>
<body>
<form action="login.php" method="post">
<p>Username
<input type="text" name="username" id="username" />
</p>
<p>Password
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" />
</p>
</form>
</body>
</html>
Here's the code for the "login.php" page:
<?php
session_start();
include('admin/misc2.inc');
$cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server" . mysqli_error());
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysqli_real_escape_string($myusername);
$mypassword = mysqli_real_escape_string($mypassword);
$result = mysqli_query($cxn,"SELECT * FROM `members` WHERE username='$myusername' AND password='$mypassword'") or die("cannot execute query");
$num = mysqli_num_rows($result);
if($num > 0)
{
$_SESSION['username'];
header("location:success.php");
}
else
echo "login fail – please click here to <a href=\"login.htm\">login</a>";
?>
Any assistance will be appreciated.