PHP, MySQL - Logging in the user
I would really appreciate some help.
I am trying to follow some tutorials on creating a login system at
http://thenewboston.org/watch.php?cat=11&number=139. With the hope of
modifying it to create my own.
It is not the finished product, just going through the processes.
I have a MySQL database with four fields. Nothing fancy, just Id,
username, password, firstname, and surname
I am working with four files:
File 1 is: 01 - Index.php
<?php
require '04 - core.inc.php';
require '02 - connect.inc.php';
include '03 - loginform.inc.php';
?>
File 2 is: 02 - connect.php
<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysql_db = "a_database";
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) ||
!mysql_select_db($mysql_db))
{
die(mysql_error());
}
?>
File 3 is: 03 - loginform.php
<?php
if(isset($_POST['username'])&&isset($_POST['password']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if(!empty($username)&&!empty($password)){
//SELECT 'username', 'password'
$query = "SELECT 'id' FROM 'users' WHERE 'username' = '$username'
AND 'password' = '$password'";
if ($query_run = mysql_query($query)){
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows==0){
echo "Invalid username or password";
}else if ($query_num_rows==1){
echo "ok";
}
}
}else {
echo "You must supply a username and password.";
}
}
?>
<form action="<?php echo $current_file;?>" method="POST">
Username: <input type="text" name="username" >
Password: <input type="password" name="password" >
<input type="submit" value="Log In!">
</form>
File 4 is: 04 - core.php
<?php
//file for predefined variables
$current_file = $_SERVER['SCRIPT_NAME'];
?>
The main file I am working on is 03 - loginform.php. The connection to the
database is working all fine.
When I do not enter any details in to the fields, I get a message "You
must supply a username and password.", as I would expect.
However, the issue that I am having is that when I enter in username and
password details which are contained in the database, the page just
refreshes and doesn't return the user's ID.
Any clues?
Thank you.
No comments:
Post a Comment