HI, I have attempted to put a login page on to my web page that i am developing as a learnig curve. I am almost there i think, i have created a table in my database that stores the username and passwords and i have a php script that i have wrote that should check the database and if the credentials are correct then let me though to the webpage. However no matter what i enter in the username and password box it says incorrect. This is the php code:

<?php session_start();

    $servername = "localhost";
    $username = "Dylanc";
    $password = "xxxx";
    $dbname = "FirstAttempt";

    // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

    $result=mysql_query("SELECT * FROM Users WHERE Username = '" . mysql_real_escape_string($_POST['Username']) . "' and Password = '" . mysql_real_escape_string(md5($_POST['Password'])) . "'");

    $row=mysql_fetch_assoc($result);

    if ($row['admin']==1) {
    $_SESSION['usersession'] = $_POST['Username'];
    header("Location: mainpage.php");
    exit;
    }

    } elseif ($row['admin']==0) {
    header("Location: index.php?error=2");
    exit;

    }

    header("Location: index.php?error=1");
    exit;

?>

I have a row in my table called admin, on the accounts that should be able to login this is set to 1 so the code should check the username and password and if the admin row is set to 1 then go thorugh to "mainpage". If admin is set to 0 this should fail and set error =2. I then have this in the index page:

<?php if($_GET['error'] == '2'): ?>
    Login Credentials are Incorrect<?php endif ?><?php if($_GET['LogOff'] == '1'): ?>Logout Successful<?php endif ?></h3>
            </caption>
            <form action="Credentials.php" method="post" name="login">
            <tr>
                <td class="menu" style="width: 394px">&nbsp;Username: </td>
                <td class="menu" style="width: 204px">
                <input class="auto-style16" name="username" style="width: 250px" type="text" size="20" /></td>
            </tr>
            <tr>
                <td class="menu" style="width: 394px">&nbsp;Password:&nbsp;</td>
                <td class="menu" style="width: 204px">
                <input class="auto-style16" name="password" style="width: 250px" type="password" size="20" /></td>
            </tr>
            <tr>
                <td class="menu" style="width: 394px; height: 23px;">&nbsp;</td>
                <td class="auto-style1" style="height: 23px; width: 204px;">
                <input class="auto-style16" name="Submit1" type="submit" value="Login" style="height: 28px" /></td>
            </tr>
            <tr>
                <td class="menu" colspan="2">&nbsp;</td>
            </tr>

So if the error=2 then dispaly incorrect credentials. I am getting that error each time so it is like the php code runs and think the admin row is set to 0 even though it isn't. Hope this makes sense. Does anyone have any suggestions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.